0

I'm trying to learn C, and now I'm playing with structures. I have the following piece of code:

#include <string.h>

struct user {
  unsigned int utime;
  char *username;
};

void main()
{
  char username[] = "root";
  struct user *u;
  strcpy(u->username, username);
}

But, when I try to run it, it generates a Segmentation fault. What's wrong with it?

Naveen
  • 74,600
  • 47
  • 176
  • 233

5 Answers5

3

u is a pointer to a struct but you didn't allocate any memory for it yet. The line must be struct user *u = malloc(sizeof(struct user)). Additionally you will also have to allocate memory for the username pointer within your struct before calling strcpy.

simon
  • 1,125
  • 1
  • 10
  • 20
  • 1
    Or just use a stack allocated `struct user` (but memory for `username` still required to by dynamically allocated). – hmjd Jun 14 '12 at 08:44
2

The first thing you should do when getting a crash (like segmentation fault), is to run your program in a debugger. It will help you pinpoint the location of the crash, and also let you examine variables to see what might have caused the crash.

However, in your case it's very simple:

struct user *u;
strcpy(u->username, username);

You haven't allocated memory for u or u->username, which means that u can point to anywhere in memory and the same for u->username.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

Two things

you have not allocated for the structure variable "struct user *u;"

and also you have not allocated memory for *username in the structore

To work

 #include <string.h>
 struct user {
 unsigned int utime;
 char *username;
 };

 void main()
 {
    char username[] = "root";
    struct user *u=malloc(sizeof(struct user));
    u->username=malloc(strlen(username)+1);
    strcpy(u->username, username);
}  

Please ignore if it has any syntax errors

Sudhakar B
  • 1,465
  • 9
  • 16
  • 1
    In pure C, return value of `malloc` doesn't have to be type-casted. – LihO Jun 14 '12 at 08:46
  • @LihO I think we should, becuase malloc return type is void*. May be you ll get warning – Sudhakar B Jun 14 '12 at 08:48
  • 1
    When allocating memory for the string, it should be `strlen(username) + 1`, so you get space for the terminating `'\0'` too. Or use `strdup` instead of `malloc` and `strcpy`. – Some programmer dude Jun 14 '12 at 08:49
  • Have a look at [Do I cast the result of malloc?](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – LihO Jun 14 '12 at 08:50
0

another way would be to use u as a simple variable: struct user u; and then access to utime or username with the ".", like this:

u.utime = ...;
u.username = ...;
lucaboni
  • 2,334
  • 2
  • 29
  • 41
0

In parallel, you should also know about memory layout, and try reasoning what lives where. A starting point for this would be -Stanford Pointers pdf I would suggest,when you are learning and get stuck,try drawing the flow, memory access on paper and then go into the debugger. I didn't do this when I learnt C first- Memory Visualization is a trait that seeems to make a person a better programmer/debugger- from what I have learnt from my colleagues/ read several articles online.

Arvind
  • 466
  • 3
  • 9