0

What does mean of "return p ? memcpy(p, s, len) : NULL;" in below code? (More generally, what is the conditional operator, a ? b : c?)

char * strdup(const char * s)
{
  size_t len = 1+strlen(s);
  char *p = malloc(len);

  return p ? memcpy(p, s, len) : NULL;
}
GKFX
  • 1,386
  • 1
  • 11
  • 30
Sensor
  • 25
  • 6
  • 3
    Please clarify which part of that expression is confusing to you. There is a lot happening there. – David Grayson Jul 29 '14 at 18:12
  • 1
    @DavidGrayson: It's in the title... and the first and only sentence in the question. How could the OP be more clear? Yeesh. – Ed S. Jul 29 '14 at 18:13
  • 2
    @EdS. maybe OP just didn't know what a ternary operator is. maybe he didn't know how you can return memcpy. it's a valid concern – Ben Jul 29 '14 at 18:14
  • 1
    We are all guessing that he doesn't know what a ternary operator is, but it's possible he is actually confused about `return`, `NULL`, or `memcpy`. He should tell us. – David Grayson Jul 29 '14 at 18:20
  • My problem is about "ternary oprator" ,Thank you guys :) – Sensor Jul 29 '14 at 18:33
  • @DavidGrayson: Seemed pretty likely that it was the ternary operator... but of course you could just explain the whole line. Not like it's terribly complicated... – Ed S. Jul 29 '14 at 18:46

3 Answers3

6

This syntax is called a ternary operator and you can think of it as of simplified if statement. return p ? memcpy(p, s, len) : NULL; is the same as:

if(p)
    return memcpy(p, s, len);
else
    return NULL;

memcpy() function returns a pointer to dest, which is a first argument of memcpy and in your case this is p. So, if p has value different than 0 (pointer is not NULL) then return that pointer. Otherwise, return NULL.

macfij
  • 3,093
  • 1
  • 19
  • 24
5

It means execute and return memcpy(p, s, len), unless p==0. If p==0, it will return NULL, and not execute memcpy(p, s, len).

Read https://en.wikipedia.org/wiki/%3F:#C for more.

Also, to paraphrase http://man7.org/linux/man-pages/man3/memcpy.3.html: The memcpy() function copies len bytes from memory area p to memory area s. The memory areas must not overlap.

That is, if we have the below memory:

   p            s
[][1][2][3][][][4][5][6][]

and len == 3, then when memcpy is called we get:

   p            s
[][1][2][3][][][1][2][3][]

Finally, the value a function returns is the value it evaluates to if you then use it in an expression; if foo() returns 5, print(foo()); prints 5.

GKFX
  • 1,386
  • 1
  • 11
  • 30
4

This is a ternary operator in C.

p ? memcpy(p, s, len) : NULL;

It means that if the first condition is true ie, p then return the value of memcpy(p, s, len) else return NULL.

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
  • `if the first condition is true ie, "memcpy(p, s, len)" is true then return the value of "memcpy(p, s, len)"` shouldn't this be `if the first condition is true ie, "p" then return the value of "memcpy(p, s, len)"`? (Ignoring other grammar issues) – Jashaszun Jul 29 '14 at 18:15
  • 1
    Also note the fact that only one of the last two expressions is evaluated, which has significant consequences. – GKFX Jul 29 '14 at 18:23
  • 2
    @RT I missed the period after your edit to undo my downvote. If you want to, can you edit your answer so that I can undo and then edit it back? – Jashaszun Jul 29 '14 at 18:23
  • 1
    @Jashaszun:- I like your spirit. Thanks +1 – Rahul Tripathi Jul 29 '14 at 18:25