C Pointers revised

I am learning to use pointers.
I have a few questions about the workout code that I wrote.

Firstly, if I have the following function code:

//function prototype void processCars(char *, char []); int main(){ ... //actual callto the function processCars(strModel[x], answer); ... } void processCars(char * string1, char string2[]) { ... } 

How would it be right to determine the arguments to this processCars function? The first - char * - is a pointer to char, which is the initial location of the string (or, better, an array of characters)? The second is actually an array of characters.

Now suppose that I want to pass several arrays of strings and even an array of structures by reference. I managed to create the following code that works, but I still do not fully understand what I am doing.

 typedef struct car { char make[10]; char model[10]; int year; int miles; } aCar; // end type // function prototype void processCars( aCar * , char **, char **, int *, int *); //aCar * - a pointer to struct of type car //char **, char ** // int * - a pointer to integer // Arrays passed as arguments are passed by reference. // so this prototype works to //void processCars( aCar * , char **, char **, int [], int []); int main(){ aCar myCars[3]; // an array of 3 cars char *strMakes[3]={"VW","Porsche","Audi"}; // array of 3 pointers? char *strModel[3]={"Golf GTI","Carrera","TT"}; int intYears[3]={2009,2008,2010}; int intMilage[3]={8889,24367,5982}; // processCars(myCars, strMakes); processCars(myCars, strMakes, strModel, intYears, intMilage); return 0; } // end main // char ** strMakes is a pointer to array of pointers ? void processCars( aCar * myCars, char ** strMakes, \ char ** strModel, int * intYears, \ int * intMilage ){ } 

So my question is how to define this "char ** strMakes". What is it, why does it work for me?

I also noticed that I cannot change part of the line, because if I correct (or the links I read) the lines are read-only. So, as in python, I can use array indices to access the letter V:

 printf("\n%c",strMakes[0][0]); 

But, unlike python, I cannot change it:

 strMakes[0][0]="G" // will not work? or is there a way I could not think of? 

So, thanks for reading my long post and a lot of questions about pointers and lines c. Your answers are appreciated.

+7
source share
1 answer

Inside the function itself, both arguments will be pointers. [] in the parameter list has absolutely no meaning, it's just syntactic sugar.

Despite the fact that there is a clear difference between the array and the pointer, the passed function array always decomposes into a pointer of the corresponding type. For example, an array of type char[3] will decrease to char * , char *[3] will decrease to char ** .

char *strMakes[3] is an array of length 3 that contains pointers to strings stored somewhere else, possibly in a read-only area of ​​the process. Attempting to change the lines themselves will result in undefined behavior, most likely a security error.

If you want to be able to modify strings, you can declare it as an array containing arrays, not pointers:

 char strMakes[3][20] = {"VW", "Porsche", "Audi"}; 

Thus, the rows will be sequentially stored within the external array.

Another way is to have an array of pointers, but pointers must point to mutable memory:

 /* these strings are mutable as long as you don't write past their end */ char str1[] = "VW"; char str2[] = "Porsche"; char str3[] = "Audi"; char *strMakes[3] = {str1, str2, str3}; 
+6
source

All Articles