Note that there are some explanatory texts on larger screens.

plurals
  1. POArray of input strings memory management in C
    primarykey
    data
    text
    <p>this is my first post on stack overflow and I would like to apologise for my english since I'm not a native english speaker. Anyway I have pretty much self-taught myself programming so obviously I am not very good at it. I got my hands into some programming exercises (got them from a friend in college) in order to get some experience but I'm having trouble with a certain one. The exercise asks to make an array of 10 names that the user will provide and one with their sales. The program should pass the first array's contents into two other arrays, one with the names that have managed more than 6000 sales and one with those that haven't managed to do so. The ones on the first array get a 500 bonus on their pays and the other ones get 200. The program should calculate the total company cost for their bonuses and print it on screen. The program should also print the two name arrays. </p> <p>UPDATE:I decided to fix the problems without using dynamic memory allocation first. SO this works perfectly but it keeps printing the last name for some reason instead of every one.</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; int c =0; int x =0; int i; int sum_500 =0; int sum_200 =0; char* names_500[10]; char* names_200[10]; char* names[10]; int sales[10]; char buffer[200]; char trash[10]; int main() { for (i =0; i&lt;10; i++) { printf("Give name:"); fgets(buffer,201,stdin); names[i] = buffer; printf("Give sales profit:"); scanf("%d",&amp;sales[i]); fgets(trash,11,stdin); if (sales[i] &gt; 6000) { names_500[c] = names[i]; sum_500 = sum_500 + 500; c++; } else { names_200[x] = names[i]; sum_200 = sum_200 + 200; x++; } } if( c&gt;0) { for(i =0; i&lt;c; i++) { printf("%s\n",names_500[i]); } } if (x&gt;0) { for(i =0; i&lt;x; i++) { printf("%s\n",names_200[i]); } } printf("Company total bonus cost:%d",sum_200 + sum_500); return 0; } </code></pre> <p>UPDATE:I figured it out. It seems that I pass the pointer to the buffer variable to the name arrays so they all print the last string. Using strdup() solves the problem. Here is the final code including dynamic memory allocation for the name arrays.</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;string.h&gt; int c =0; int x =0; int i; int sum_500 =0; int sum_200 =0; char ** names_500; char ** names_200; char * name; int sales[10]; char buffer[40]; char trash[10]; int main() { for (i =0; i&lt;10; i++) { printf("Give name:"); fgets(buffer,40,stdin); name = strdup(buffer); printf("Give sales profit:"); scanf("%d",&amp;sales[i]); fgets(trash,11,stdin); if (sales[i] &gt; 6000) { names_500= realloc(names_500,(c+1)*sizeof(char*)); names_500[c] = name; sum_500 = sum_500 + 500; c++; } else { names_200= realloc(names_200,(x+1)*sizeof(char*)); names_200[x] = name; sum_200 = sum_200 + 200; x++; } } if( c&gt;0) { printf("500 bonus :\n"); for(i =0; i&lt;c; i++) { printf("%s",names_500[i]); } } if (x&gt;0) { printf("200 bonus:\n"); for(i =0; i&lt;x; i++) { printf("%s",names_200[i]); } } printf("Company 200 bonus costs:%d",sum_200 ); printf("\nCompany 500 bonus costs:%d",sum_500); return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload