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.