-2

I cannot seem to find a way to initialize a struct without getting segmentation fault . Here is the assignment `

int id = 5;
// scanf ("%10d", &id);
printf("Please give an name\n");
char *tmpName = (char*)malloc(MAXSTRING * sizeof(char));
fgets(tmpName,MAXSTRING,stdin);
student newStudent = (student){ .id = id , .name = tmpName };
printf("%d",newStudent.id);
printf("%s",newStudent.name);

` And here is the struct itself

#define MAXSTRING  256
typedef struct{
    char *name;
    int id;
}student;

I can successfully initialize the struct , but I cannot get access to the name variable , any thoughts?

EDIT : Answers submitted at the time offered nothing , the problem was the way the struct was initialized

char tmpName[MAXSTRING] =  {0};
scanf("%s",tmpName);
student newStudent = { .id = id };
strcpy(newStudent.name,tmpName );

This block fixed the issue , will close the topic.

Naoum Mandrelas
  • 258
  • 3
  • 20
  • What do you think where the scanf is supposed to write? How about mallocing something? – Yunnosch Apr 22 '18 at 14:11
  • You mean something like char *tmpName = malloc( sizeof(char)) . Sorry, I cannot wrap my head around memory allocation. – Naoum Mandrelas Apr 22 '18 at 14:13
  • I added an edit to my answer to adress that part. – Alceste_ Apr 22 '18 at 14:18
  • @MakisMandrelas You are clearly not that new to SO. There are no topics here. Questions don't get edited to include information about your solution. Questions are questions, answers are answers. There is no such thing as a 'topic'. – dandan78 Apr 22 '18 at 16:37

2 Answers2

1

You define a pointer

char *tmpName;

you do nothing to make it actually point to some useable space, especially there is no malloc() or similar.
The you have scanf() (if successful...) write to the pointer, but a string, not any meaningful address. I.e. if that string is not extremely short, you certainly write beyond.

scanf("%s",&tmpName);

Do not be surprised by segfaults, be surprised if there are none.
Later on you then read from where that weird non-pointer points to...

To solve, insert a malloc() after the pointer definition. Alternatively use fixed array of char as an input buffer.
Use the pointer itself, not its address, in scanf() to write the string into the malloced space. (Or the array identifier.)

The rest is the typical set of problems with having enough space, failing to scan, without checking return value etc. Here is a great set of basic hints for getting input right:
How to read / parse input in C? The FAQ

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
-2

When you declare char name[MAXSTRING], you are telling your compiler that it will always have the exact length of MAXSTRING.

When the compiler hears that, he will most likely replace your string with a lot of static char members.

#define MAXSTRING  4
typedef struct{
    char name[MAXSTRING];
    int id;
}student;

Would be extended and compiled more like so :

#define MAXSTRING  4
typedef struct{
    char name0;
    char name1;
    char name2;
    char name3;
    int id;
}student;

Although you would still need to access those using the syntax name[index], as you declared an array, the array pointer is in fact nowhere to be found. So you can't directly assign it.

This is basically what happens with any fixed-length array, they won't allow you to asign them since their content is directly stored in the stack or in the datastructure enclosing it.

You would need to declare.

typedef struct{
    char *name;
    int id;
}student;

To get a char array pointer of variable length that you can assignate.


That should answer your question, now, as stated by someone in your comment section, there are other things wrong in your code and even with the correct structure, you will probably be unnable to do what you want since scanf is probably returning an arror right now. ^^'

Try allocating the memory beforehand doing so :

char *yourstring = (char*)malloc(MAXSTRING * sizeof(char));

And of course, don't forget to free when you don't need either the string or your student structure :

free(yourstring);

Then again, there's another small issue with your assignement since you do something like this :

.name = *yourstring

While using pointers, prefixing them with * will access the value at the pointed address.

Supposing char *yourstring = "I love cats.";
Doing .name = *yourstring will result in .name being equal to 'I' character, which is first in the array pointed to by yourstring. (Most compiler would complain and ask you to cast.. do not do that.)

So what you really need to do is to assign the pointer to your .name array pointer.

.name = yourstring

Alceste_
  • 592
  • 4
  • 13
  • 1
    There is a lot of non-sense in your answer. – Paul Ogilvie Apr 22 '18 at 14:24
  • `char name0;char name1;...`: makes no sense, so remove it. Don't cast the result of malloc; `char name[]` is wrong and `.name = *yourstring;` will make the compiler complain. Better to remove your answer. – Paul Ogilvie Apr 22 '18 at 14:27
  • Please learn about pointers, arrays and structures before answering such questions. Not only you confuse pointres and arrays for the same (they are not), you also state a lot of wrong resp. questionable claims. Plus proposing invalid declarations, etc. If you learned it that way yourself, get your money back for the course or stop trying to learn from obscure youtube videos or online tuorials. Btw: the edit did not really make it any better. – too honest for this site Apr 22 '18 at 14:31
  • Ah, you are right about the char name[], dunno I picked that from which language. I however add the other version right next to it and was saying he should use the `char *name`, so I think you're a bit harsh. `.name = *yourstring` won't compile, but I've said that right beside the code.. I don't agree with most of your reproach. :c – Alceste_ Apr 22 '18 at 14:31
  • Olaf : I'm usually refering to arrays as array pointer, I think this is what makes it the easiest to follow for beginners while still remembering them that there is a pointer working around that. (Though arrays aren't pointers, arrays use pointers heavily). Then again, the part with the invalid assignation is a copy/paste from his code, and is followed by "this won't compile." I think you are being unfair while not having read the answer. – Alceste_ Apr 22 '18 at 14:35