0

Hi,I am trying to scan a string to char *name inside struct data but it gives me segmentation fault.
please help
thanks.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <string.h>
struct data
{
	char *name;
};
typedef struct data  data;
int main(void)
{

data first;
data *fp;
fp=&first;
char *word;
word="ABC";
sscanf(word,"%s",(char *)fp->name);// I need to scan a string to char *name inside struct data
printf("fp is %s\n",fp->name);

return 0;
}
2
Contributors
2
Replies
5
Views
6 Years
Discussion Span
Last Post by Ahmed Sarwat
0

pointer is not initialized ..
Following code ay help ..

#define MAX 100

data first;
data *fp;

first.name = malloc(MAX);

fp = &first;

scanf("%s",first.name);
printf("fp is %s\n",fp->name);
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.