0

I cannot retrieve string using printf . instead getting error : segmentation fault

int main(){

        char * a[5];
        int i;

        printf("\n enter value ");

        for (i=0;i<5;i++){
                printf("%d Name :\n",i);
                scanf("%s",&a[i]);
        }

        printf("%s",a[2]);
        printf("%s",a[3]);

}
~     
simon bremson
  • 93
  • 2
  • 10
  • 2
    You have an array of pointers and do not allocate any memore for them. Accessing memory via unitialized pointers is undefined behaviour and may result in seg fault. – Gerhardh Apr 13 '18 at 10:42
  • 2
    Do you want to enter and print 5 names, or enter and print 1 name with 5 characters? Start with always allowing room for a NULL character in your string (char array), allocate it using a[6] instead of 5. – Grantly Apr 13 '18 at 10:43

3 Answers3

2

char * a[5]; is array of 5 char pointer that means you can store 5 char buffer of any length. The problem is in the statement scanf("%s",&a[i]); replace this with scanf("%s",a[i]);

case 1 :-

int main(){
        char *a[5] = {"stackoverflow","meta","ask ubuntu","super","unix"};
        /* here a[row] can have any number of char */
        for (int i = 0 ;i < 5;i++) {
                printf("%s\n",a[i]);
        }
        return 0;
}

Case 2 :-

int main(){
        char *a[5];
        for (int i=0;i<5;i++){
            printf("%d Name :\n",i);
            a[i] = malloc(MAX);/* define MAX how many char you want to store into that */
            scanf("%s",a[i]); /* & not required as a[i] itself address */
        }
        for (int i = 0 ;i < 5;i++) {
                printf("%s\n",a[i]);
        }
        /* don't forget to free once job is done */
        for (int i = 0 ;i < 5;i++) {
                free(a[i]);
        }
        return 0;
}
Achal
  • 11,821
  • 2
  • 15
  • 37
0

The problem is you are using a pointer before it has been initialized. It does not yet point at valid memory. These leads to program crashes or other kinds of unexpected behavior, such as "segmentation faults".

So you should edit it to:

char* my_string = malloc(size+1);

The %c format specifier is used whenever we want to be specific that the variable that we are going to printf or scanf is of type char.

On the other hand the %s format specifier is used to specify to the printf or scanf functions that contents of the address that is specified as the next parameter are to considered as string.

You are taking char as input, not the whole string.

scanf("%s",&a[i]);, will be scanf("%c",&a[i]);
printf("%s",a[3]);, will be printf("%c",a[3]);
printf("%s",a[2]);, will be printf("%c",a[2]);

The following code worked fine:

#include<stdio.h>
#include<stdlib.h>

int main(){

        char* a = malloc(size+1); // where size is max string length
        int i;

        printf("\n enter value ");

        for (i=0;i<5;i++){
                printf("%d Name :\n",i);
                scanf("%c",&a[i]);
                printf("%c\n",a[i]);
        }

        printf("%c\n",a[2]);
        printf("%c\n",a[3]);
        free(a);
}
Lundin
  • 195,001
  • 40
  • 254
  • 396
Abhishek Keshri
  • 3,074
  • 14
  • 31
0

You shouldn't input to a char* . And you shouldn't store any string by char*, including string literal, in fact, string literal convert to char* is deprecated many years ago. Use char a[5][MaximumLength] instead. MaximumLength should be a literal.

con ko
  • 1,368
  • 5
  • 18