I worked on passing pointers to an array in C
and it broke my heart. I assumed the size of the array could have been retrieved from the array if I got a pointer to the entire array rather than just the pointer to the first element in it. After a bunch of digging it turns out I was wrong. Another day, another painful lesson in C
TLDR;
Okay, so here are the highlights of what I did:
- Played around with my reverse array function to see if I can improve it. The code got really messy with some notes at the bottom of the program but it is included below.
Reverse Array Program (Messy Code FYI)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void reverseArray(char *origin, char *target, int size);
int main(){
char revStr[10];
char str[10];
int number, R;
number = R = 1500;
// Method 1: Loop and divide out each digit to copy ANSI
// Unfortunately I can't figure out how to get this working correctly
int size = sizeof(str);
for(int i=0; i<size; ++i){ // Paint over str memory
// Mark end of string at last value
if(i == (size - 1)){
revStr[i] = '\0';
str[i] = '\0';
break;
}
revStr[i] = ' ';
str[i] = ' ';
}
printf("Empty strings arrays:\n");
printf("revStr: '%s'\n", revStr);
printf("str: '%s'\n", str);
printf("---------\n");
for(int i=0;; ++i){ // Copy digits into str as ANSI digit chars
if(R <= 0)
break;
revStr[i] = '0' + (R % 10);
R = (int)(R / (float)10);
printf("R = %i, Char = %c\n", R, revStr[i]);
}
printf("result: %s\n", revStr);
reverseArray(revStr, str, size);
// printf("Reversed result: %s\n", str);
// Method 2: use the sprintf function
// sprintf(str, "%i", number);
// printf("---------\nMethod 2:\n");
// printf("result: %s\n", str);
return 0;
}
void reverseArray(char *origin, char *target, int size){
int last = size - 1;
int start = 0;
char temp;
printf("\nsize of the array: %i", size);
printf("\nfirst character of the array: %i,\t char = '%c'", *origin, *origin);
printf("\nlast = %d", last);
printf("---------\n\n");
while(start < last){
// Something is wrong and the blanks are not getting skipped or recognized
printf("start: %d\tlast: %d\n", start, last);
printf("character = '%c', %i\n", origin[last], origin[last]);
if(origin[last] > '9' || origin[last] < '0'){
printf("character to skip = '%c'\n", origin[last]);
--last;
continue;
}
temp = origin[last];
target[start] = origin[last];
target[last] = origin[start];
++start;
--last;
printf("original: %s\treversed = %s\n", origin, target);
}
}
/*
* Notes:
*
* To declare the parameters for a function as pointer to an array we must cast it as a pointer from the array type definition.
* Example for a pointer to an entire character array:
* void reverseArray(char (*originArrP)[], char (*targetArrP)[]);
*
* However when we pass in the arrays address to the function as an argument we will lose access to the size of the array with
* this definition. The pointer with specific size details cannot be used to determine the size of the array.
* Only the original pointer has that value.
* ... I think. When I tried to calculate the size using sizeof(*originArrP); I got an error:
*
* 1_example.c: In function 'reverseArray':
* 1_example.c:64:46: error: invalid application of 'sizeof' to incomplete type 'char[]'
* printf("size of *originArrP = %d\n", sizeof(*originArrP));
*
* Example for a pointer to an entire character array with access to the size of the array still:
* void reverseArray(char (*originArrP)[10], char (*targetArrP)[10]);
*
* So my most realistic option to be flexible is include the array's size as a parameter and just take
* the pointer for the arrays elements to work with. I get nothing out of taking in pointers to the
* original array as an argument. I thought it would be helpful but it's not which sucks!
*
* https://stackoverflow.com/questions/492384/how-to-find-the-sizeof-a-pointer-pointing-to-an-array
*
* */
# Output:
Empty strings arrays:
revStr: ' '
str: ' '
---------
R = 150, Char = 0
R = 15, Char = 0
R = 1, Char = 5
R = 0, Char = 1
result: 0051
size of the array: 10
first character of the array: 48, char = '0'
last = 9---------
start: 0 last: 9
character = '', 0
character to skip = ''
start: 0 last: 8
character = ' ', 32
character to skip = ' '
start: 0 last: 7
character = ' ', 32
character to skip = ' '
start: 0 last: 6
character = ' ', 32
character to skip = ' '
start: 0 last: 5
character = ' ', 32
character to skip = ' '
start: 0 last: 4
character = ' ', 32
character to skip = ' '
start: 0 last: 3
character = '1', 49
original: 0051 reversed = 1 0
start: 1 last: 2
character = '5', 53
original: 0051 reversed = 1500
Goal For Round 6 of the #100DaysofCode Challenge
This is my sixth round of the “#100daysofcode” challenge. I will be continuing my work from round five into round six. I am currently working through the book “Cracking the Coding Interview” by Gayle Laakmann McDowell. My goal is to become more familiar with algorithms and data structures. This goal was derived from my goal to better understand operating systems and key programs that I use in the terminal regularly e.g. Git. This goal was in term derived from my desire to better understand the fundamental tools used for coding outside of popular GUIs. This in turn was derived from my desire to be a better back-end developer.
I have no idea if my path is correct but I am walking down this road anyways. Worst case scenario I learn a whole bunch of stuff that will help me out on my own personal projects.