frankx: Anfängerfrage zu Strings in C

Beitrag lesen

Hellihello Zusammen,

brauch ich nicht einen char*** um den Parameter zu manipulieren?

Ich habe versucht, das Problem zu isolieren:

  
/*  
* get adress of pointer of strings (char**)  
* and fill with a little content.  
* parameter mystrings ist actually "&mystrings", adress of mystring  
*/  
int fillList(void* mystrings) {  
 //pointer to pointer of strings, to manipulate the content under the given adress  
 char*** mystrings_tmp;  
 // pointing to passed adress of mystrings  
 mystrings_tmp = mystrings;  
 //gibt some size for memory  
 int mysize = 5;  
 printf("----start fillList\n");  
 printf("----before malloc\n");  
 // some controlling/comparing of adresses and content  
 printf("fillList: adresse &mystrings = %p\n", &mystrings);  
 printf("fillList: adresse mystrings = %p\n", mystrings);  
 printf("fillList: adresse &mystrings_tmp = %p\n", &mystrings_tmp);  
 printf("fillList: adresse mystrings_tmp = %p\n", mystrings_tmp);  
 printf("fillList: adresse mystrings_tmp[0] = %p\n", mystrings_tmp[0]);  
 // set the conent of mystring_tmp[0], which is  
 // to some memory (in this case space for 5 pointer to char*, only two used in example  
 mystrings_tmp[0] = (char**) malloc(mysize * sizeof(char*));  
 // controlling again adresses and content  
 printf("----after malloc\n");  
 printf("fillList: adresse &mystrings = %p\n", &mystrings);  
 printf("fillList: adresse mystrings = %p\n", mystrings);  
 printf("fillList: adresse &mystrings_tmp = %p\n", &mystrings_tmp);  
 printf("fillList: adresse mystrings_tmp = %p\n", mystrings_tmp);  
 printf("fillList: adresse mystrings_tmp[0] = %p\n", mystrings_tmp[0]);  
 //allocating memoryspace for two strings with maxlength of 4 chars (mysize = 5 incl. \0)  
 //setting both with some values:  
 mystrings_tmp[0][0] = (char*) malloc(mysize * sizeof(char*));  
 strcpy(mystrings_tmp[0][0], "abcd");  
 mystrings_tmp[0][1] = (char*) malloc(mysize * sizeof(char*));  
 strcpy(mystrings_tmp[0][1], "hijk");  
 printf("----end fillList\n");  
}  
int main() {  
 //my string list - empty, not initialized  
 char** mystrings;  
 //check Adress of var and content  
 printf("main: adresse &mystrings = %p\n", &mystrings);  
 printf("main: adresse mystrings = %p\n", mystrings);  
 printf("call fillList\n");  
 // call fillList passing adress of mystrings:  
 fillList(&mystrings);  
 //check Adress of var and content afterwards:  
 printf("main: adresse &mystrings = %p\n", &mystrings);  
 printf("main: adresse mystrings = %p\n", mystrings);  
  
 // testing output should b : abcd and hijk  
 printf("main: content mystrings[0] = %s\n", mystrings[0]);  
 printf("main: content mystrings[1] = %s\n", mystrings[1]);  
 //free allocated space  
 printf("free:\n");  
 free(mystrings);  
}  

ergibt:

main: adresse &mystrings = 0x22cce4
main: adresse mystrings = 0x401560
call fillList
----start fillList
----before malloc
fillList: adresse &mystrings = 0x22ccc0
fillList: adresse mystrings = 0x22cce4
fillList: adresse &mystrings_tmp = 0x22ccb0
fillList: adresse mystrings_tmp = 0x22cce4
fillList: adresse mystrings_tmp[0] = 0x401560
----after malloc
fillList: adresse &mystrings = 0x22ccc0
fillList: adresse mystrings = 0x22cce4
fillList: adresse &mystrings_tmp = 0x22ccb0
fillList: adresse mystrings_tmp = 0x22cce4
fillList: adresse mystrings_tmp[0] = 0x670160
----end fillList
main: adresse &mystrings = 0x22cce4
main: adresse mystrings = 0x670160
main: content mystrings[0] = abcd
main: content mystrings[1] = hijk
free:

was erstmal so ausschaut, als würde es stimmen...;

Frage nur, ob das von hinten durch die Brust ins Auge oder ob das einfacher gingen...;

Dank und Gruß,

frankx

--
tryin to multitain  - Globus = Planet != Welt