-3

I have a struct as following:

extern struct team_t
{
    char *name1;
    char *email1;
    char *name2;
    char *email2;   
} team;

struct team_t team =
{
    "some string1",
    "some string2",
    "some string3",
    "some string4"
};

Then in another file I create the following function that copies this struct into a new struct:

void *ucase( struct team_t *team)
{
  struct team_t *ucase_team = malloc( sizeof *ucase_team);

  memcpy ((char*)ucase_team, (char *)team, sizeof (ucase_team));

  return NULL;
}

However, when I want to call ucase(team), I'm getting a segfault. I need to use void * because this will later be used for shell signals. What am I missing?

Update: Following call gives type argument of unary ‘*’ (have ‘struct team_t’) error:

ucase(*team)

Update 2: I have removed Null return and used ucase(team) but still getting segfault.

feedMe
  • 3,431
  • 2
  • 36
  • 61
dany
  • 3
  • 2
  • 4
    `sizeof (ucase_team)` will just returns `size of pointer`. – kiran Biradar Sep 13 '18 at 13:33
  • Possible duplicate of [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – hellow Sep 13 '18 at 13:34
  • 1
    You need to provide more code, e.g. show how you call your function. There's a few bugs in your current function (such as `sizeof (ucase_team)` in the `memcpy()` call should be `sizeof (*ucase_team)`, and you also return NULL so you leak memory) - but nothing that should cause a segfault, unless you have passed some invalid value to your `ucase` function. Please create a full example that can be compiled/run to showcase your problem. – nos Sep 13 '18 at 13:35
  • 1
    Where in your code does it crash? – Chris Turner Sep 13 '18 at 13:44
  • 1
    How `team_t` is defined? Show [mcve]. – Eugene Sh. Sep 13 '18 at 13:46
  • 1
    You know you have a memory leak there, don't you? Oh, and the casts to `char*` are pointless in `memcpy()` call. – Toby Speight Sep 13 '18 at 13:47
  • Perhaps you are trying to use the return value of this function? Which is.... – Eugene Sh. Sep 13 '18 at 13:48
  • Please see my update. I have showed team_t structure and my call – dany Sep 13 '18 at 13:50
  • 1
    Your update is inconsistent with the question. Decide what error you are having. And post [mcve]. – Eugene Sh. Sep 13 '18 at 13:51
  • 1
    `team` is a struct and cannot be dereferenced. What you need is `&team` instead of `*team`. But if you got a compile error, you cannot get a sefgault. – Gerhardh Sep 13 '18 at 13:51
  • Ok, VTC for the lack of [MCVE]. – Eugene Sh. Sep 13 '18 at 13:53
  • 1
    If you'd be really doing C those cast when calling `memcpy()` were useless. If the compiler insist on them you most likely are doing C++. – alk Sep 13 '18 at 14:07

1 Answers1

1

The last argument of memcpy() should be sizeof(struct team_t) instead of sizeof (ucase_team) as ucase_team is a struct pointer variable. It can be either sizeof(*ucase_team) or sizeof(struct team_t).

Also calling team() function like

ucase(*team);

is wrong as team is not a variable of pointer type, it's a normal structure variable. Probably you want

ucase(&team);

Achal
  • 11,821
  • 2
  • 15
  • 37