0

I'm getting confused working with character string arrays. I'm trying to fill 2 arrays in a for loop. Within each array, all elements are the same.

To conserve memory, for array_person_name I attempt to simply copy the pointer to the string stored by person_name. For array_param, the string that it stores the pointer to is always 2 characters long (e.g. "bt") plus the null termination character , and here I also attempt to conserve memory by storing the pointer to "bt" in array_param.

Since the number of array elements, arraysize, is downloaded from a database when the program runs, I use malloc to allocate memory. Since my OS is 64 bit (Linux x86-64), I allocate 8 bytes for each of arraysize pointers. 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 ); /* 8 is for 64b OS */
array_param = malloc( 8 * arraysize ); 
for (kk = 0; kk < arraysize; kk++) {
    array_person_name[kk] = &person_name;
    array_param[kk] = &"bt";
}

/* test results by printing to screen */
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 the warnings: warning: assignment makes integer from pointer without a cast on the two lines inside the for loop. Any idea what I'm doing wrong?

3
  • 3
    Don't "guess" the size of the pointers but use sizeof(char *) Commented Apr 7, 2012 at 16:06
  • 1
    If you really want to use strncpy() and zero-fill the tail end of your person_name array (again, because the initializer also zero-filled it), then use: strncpy(person_name, "John Smith", sizeof(person_name));. In general, using sizeof() helps. You do need to be cautious about whether you have an array name or just a pointer. sizeof(array) gives the size of the array, but sizeof(pointer) gives the size of a pointer, not the size of the array. Function arguments are pointers, not arrays. Be cautious about using sizeof() on function arguments, and pass arrays sizes with strings. Commented Apr 7, 2012 at 16:13
  • Thanks Jonathan, I included the 100 in strncpy to avoid potential errors if more than 100 characters are copied (I'm not trying to pad with spaces). My intent is to simply copy John Smith into person_name, along with a null character automatically included by the compiler. I'm not sure if your comment impacts this. Commented Apr 7, 2012 at 16:30

3 Answers 3

3

Since you want each item in array_person_name and array_param to be a pointer to person_name/"bt", you want a char **:

char **array_person_name;

array_person_name = malloc(arraysize * sizeof(*array_person_name));

for (int i=0; i<arraysize; i++)
    array_person_name[i] = person_name;
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Jerry, what should I use for malloc-ing array_param? Can I do , for example, sizeof(*"bt")?
You almost always want ptr = malloc(sizeof(*ptr) * item_count);
1

You're assigning a pointer to array person_name to character defined by array_person_name[kk]. What you probably meant to do was to define array_person_name as a char** type.

Comments

1

You shouldn't be assuming 8 bytes because it's 64 bit. You should leave that part to C and use sizeof() operator.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.