C: Cannot declare pointer inside if statement

Posted by echedey lorenzo on Stack Overflow See other posts from Stack Overflow or by echedey lorenzo
Published on 2010-03-21T18:16:30Z Indexed on 2010/03/21 18:21 UTC
Read the original article Hit count: 285

Filed under:
|

Hi,

I have a pointer which points to a function. I would like to:

  if (mode == 0)
  {
  const unsigned char *packet = read_serial_packet(src, &len);

  } else {
  const unsigned char *packet = read_network_packet(fd, &len);
  }

But I cannot do it because my compiler complains when I first use the pointer later in the code.

   error: 'packet' undeclared (first use in this function)

This is strange. It worked without the if statement, but now I need my program to be able to get data from different sources. Isn't it possible to do this? I think so. If it isn't, is there any other simple way to get what I am trying?

Thanks a lot.

© Stack Overflow or respective owner

Related posts about c

    Related posts about conditional