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;
}

Recommended Answers

All 2 Replies

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);

Thanks..now it is working

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.