-1
#include<stdio.h>
struct st
{
    char *name;
    char *br;
};
int main()
{
     struct st ob[2];
     ob[0].name=NULL;
     ob[0].br=NULL;
     ob[1].name=NULL;
     ob[1].br=NULL;
     printf("Enter name:");
     scanf("%s",ob[0].name);
     printf("enter branch:");
     scanf("%s",ob[0].br);
}

When I execute this program the program stops executing after I enter the name. Please suggest what changes be made so that the program works fine.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Ajax
  • 17
  • 3

4 Answers4

3
struct st
{
    char *name;
    char *br;
};

Before scanning you need to allocate memory to the pointers in your structure.

Use malloc() to allocate memory.

ob[0].name=malloc(20);
ob[0].br=malloc(20);
Gopi
  • 19,784
  • 4
  • 24
  • 36
3
   Before Getting a value from the user you must allocate a memory location for that

pointers using malloc() function or calloc() function. If you are not allocating it will through a error as segmentation fault.

ob[0].name= (char *)malloc(sizeof(char) * size of string);

 ob[0].br=  (char *) malloc(sizeof(char) * size of string);

 ob[1].name= (char *) malloc(sizeof(char) * size of string);

 ob[1].br=   (char *)malloc(sizeof(char) * size of string);
Bhuvanesh
  • 1,269
  • 1
  • 15
  • 25
1

As @Gopi pointed out, you need to allocate memory for your pointers. This can be done using theMalloc() function.

#include<stdio.h>
struct st
{
    char *name;
    char *br;
};
int main()
{
     struct st ob[2];
     ob[0].name= malloc(sizeof(char) * SIZE OF STRING);
     ob[0].br=   malloc(sizeof(char) * SIZE OF STRING);
     ob[1].name= malloc(sizeof(char) * SIZE OF STRING);
     ob[1].br=   malloc(sizeof(char) * SIZE OF STRING);
     printf("Enter name:");
     scanf("%s",ob[0].name);
     printf("enter branch:");
     scanf("%s",ob[0].br);
}    
MrSykkox
  • 528
  • 4
  • 14
  • No need to cast malloc() – Gopi Dec 03 '14 at 05:34
  • 1
    Take a look at this: [Do I need to cast the result of malloc?](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – James H Dec 03 '14 at 05:35
  • As pointed out by Deva, I gave the '&' sign before ob[0].name and it worked. I am surprised how it works. A pointer keeps addresses of the strings that we enter, then what is the need of a '&' operator here. – Ajax Dec 03 '14 at 05:45
0

You forgot & opetator in scanf function.

Deva
  • 48
  • 6
  • when pointers keep addresses of the strings that we enter, what is the need of '&' sign here. Pl. comment – Ajax Dec 03 '14 at 05:25
  • @Ajax I don't mind accepting this as answer but why is even`&` required to scan a string here. And also this ans doesn't speak about the actual issue on hand which is allocating memory. – Gopi Dec 03 '14 at 05:53
  • @Ajax Argument of scanf works like a pointer so you have to point to what you are getting input for,that's why. While Ob[0].name will get you the value and it will overwrite the wrong sections of memory, rather than pointing to the memory location of the variable you are attempting to get input for. I hope this will satisfy your questions. – Deva Dec 03 '14 at 06:19
  • @Gopi The answer was straightforward and that is the reason I accepted that. However, the allocation of memory to the objects that we create is of prime importance. I completely agree with you. Thanks. – Ajax Dec 04 '14 at 09:10
  • @Ajax `Adding a & made your code work` is worng. Even if you don't add `&` your code should work thats what I am saying – Gopi Dec 04 '14 at 10:06
  • @Ajax Without the ampersand (&) your code is working. If you put the ampersand for the string value to get the input it will throw the warning message. – Karthikeyan.R.S Dec 06 '14 at 06:50