I am confused about working with character arrays. I am trying to populate 2 arrays in a for loop. Inside each array, all elements are the same.
To save memory, for array_person_nameI'm trying to just copy the pointer to a string stored in person_name. For the array_paramline in which it stores the pointer, it always has a length of 2 characters (for example, "bt") plus a zero-termination character, and here I am also trying to save memory by saving the pointer to "bt" in array_param,
Since the number of array elements is arraysizeloaded from the database when the program starts, I use malloc to allocate memory. Since my OS is 64-bit (Linux x86-64), I allocate 8 bytes for each of the pointers arraysize. Although not shown, I free these two arrays at the end of the program.
int kk, arraysize;
char person_name[101] = "";
char * array_person_name;
char * array_param;
...
strncpy(person_name, "John Smith", 100);
arraysize = <this value is downloaded from database>;
...
array_person_name = malloc( 8 * arraysize );
array_param = malloc( 8 * arraysize );
for (kk = 0; kk < arraysize; kk++) {
array_person_name[kk] = &person_name;
array_param[kk] = &"bt";
}
printf("Here is array_person_name[0]: %s\n", array_person_name[0]);
printf("here is array_param[0]: %s\n", array_param[0]);
The compiler returns warnings: warning: assignment makes integer from pointer without a castfor two lines inside a for loop. Any idea what I'm doing wrong?
source
share