1

i am going to Write a program that stores strings and print the last two strings (i shoulde Use array of pointers).

this is my code

#include<stdio.h>
#include <string.h>
main()
{
    char*arr[10];
    char student_name[20];
    int i,j;

    for(i=0;i<10;i++) {
        printf("Enter the name of student %d : ",(i+1));
        scanf("%s",student_name);
        arr[i]=student_name;
    }

    for(j=7;j<10;j++) {
        printf("the name of student %d : %s\n",(j+1),arr[j]);
    }
}

it just stores the last string and prints it

this is the samle run

Enter the name 1 : qqq
Enter the name 2 : www
Enter the name 3 : eee
Enter the name 4 : rrr
Enter the name 5 : ttt
Enter the name 6 : yyy
Enter the name 7 : uuu
Enter the name 8 : ii
Enter the name 9 : ioo
Enter the name 10 : ppp
the name 9 : ppp
the name 10 : ppp

what is my mistake?

also if i replase

arr[i]=student_name;

with

strcpy(arr[i], student_name);

the sumple run will be

Enter the name 1 : ddf
Segmentation fault (core dumped)
1
  • Welcome to Stackoverflow. Your question is clear and well formatted; I hope you will get a quick answer. Commented Nov 2, 2015 at 20:52

2 Answers 2

3

What you're doing is just assigning the pointer of student_name to all your array elements which will be replaced on every iteration in your while loop. Instead you should use strdup to save the string in your array. To prevent overflow bugs you should truncate the student name at 19, since the length of the array is 20 which will have the nul terminator. This can be done using the special formatting %19s.

You never allocate memory for the strings which you must do with either using malloc or by using the strdup function to do it for you.

for(i=0;i<10;i++){
  printf("Enter the name of student %d : ",(i+1));
  scanf("%19s",student_name);
  arr[i] = strdup(student_name);
}

Also you should free the memory before exit. Which you can do after you print the strings.

for(i=0;i<10;i++){
  free(arr[i]);
}
Sign up to request clarification or add additional context in comments.

5 Comments

@WhozCraig My bad! Sorry I confused strcpy with strdup. ´Fixed it now.
No worries. Its better, Could still use verifying that scanf worked and limiting the read-size to 9+1, but its better than it was.
is there another way which i can use the function strcpy?
@user5517410 please refer to the manual
@user5517410 yes, you'd have to manually allocate memory to copy the content of student_name to the array. So you would add arr[i] = malloc(20); before scanf("%19s",student_name);. See the answer by Giorgi for an explanation.
3

Indeed you have array of pointers. If you want to write something to memory where each pointer points, you need to assign some valid memory - where you could write - to each of these pointers. You could do this:

for(int i = 0; i <10; i++)
  arr[i] = malloc(MAX_LEN); // Initialize each pointer to point to valid memory

Now, you can copy the string user enters to memory where each pointer points to:

scanf("%s",student_name);
strcpy(arr[i],student_name);

This way we are copying the contents what user entered. The way you had it - you were setting each pointer in the array point to the same address - address of first element of array student_name. Hence all pointers in the array were pointing to same memory location (that memory location stays the same though, that is where student_name "lives"). And if in such case you try to print contents of each pointer, they will all print same value - data which is stored at address student_name.

Finally, free each pointer later in the array using loop.

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.