views:

334

answers:

8

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.

+2  A: 

You can't declare variables inside an if statement and then use them later, for the simple reason that this would be a non existent memory location for some paths through the program.

EDIT: So declare it earlier.

Rob Lachlan
+6  A: 

Variables declared in a block aren't seen outside it.

  if (mode == 0)
  {
    const unsigned char *packet = read_serial_packet(src, &len);
    // packet can be used here
  } else {
    const unsigned char *packet = read_network_packet(fd, &len);
    // packet can be used here
  }

  // packet can not be used here

You must declare packet before the if and assign it in if and else according to your condition:

const unsigned char *packet;

if (mode == 0)
{
  packet = read_serial_packet(src, &len);
}
else
{
  packet = read_network_packet(fd, &len);
}

// packet can be used here
Eli Bendersky
`const` is still OK in the proper usage.
Potatoswatter
@Potatoswatter: you're right. added
Eli Bendersky
+14  A: 

You need to review the concept of blocks or compound-statements, or variable scope.

If you declare a variable in a compound statement ({, }), the variable will be declared only in that exact scope.

Thus, change your code to

const unsigned char *packet = NULL;
if (mode == 0)
{
   packet = read_serial_packet(src, &len);
} else {
   packet = read_network_packet(fd, &len);
}

// Now you can perform operations on packet. 
mnemosyn
So that was the reason why the compiler was complaining about the undeclared *packet. Your code worked perfecly. Thanks a lot.
echedey lorenzo
+1  A: 

The declaration of the pointer inside if statement clause scopes it up to the end of the if statement. You should declare your pointer outside if statement and do the assignment inside.

Eimantas
+2  A: 

When you define a variable, you can only use it within the defined scope.

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

    // packet goes out of scope when the block ends
} 

So you need to move your definition to the biggest scope you need:

{
    const unsigned char *packet;

    {
        packet = read_serial_packet(src, &len);

    }

    // packet still in scope here

}

// and packet is no longer in scope here 
R Samuel Klatchko
+2  A: 

Declare the pointer before the if statement as:

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

} else {
    packet = read_network_packet(fd, &len);
}
codaddict
+2  A: 

This is because curly braces { } make a new scope. This means that every variable defined between these braces is defined within a scope and hence not visible outside of it.

To make it work you need to define *packet before if-else:

const unsigned char *packet = 0;
if (mode == 0)
{
    packet = read_serial_packet(src, &len);
} 
else {
    packet = read_network_packet(fd, &len);
}
pajton
The characters inside the packet (after the pointer) won't change. The pointer may change anytime.
Potatoswatter
I second @Potatoswatter's comment: you can still declare the pointer to `unsigned char const` in this example.
PP
@Potatoswatter, @PP thanks, of course you are right.
pajton
+7  A: 

Any kind of brackets will put it out of scope, even if not a construct like for, while, or if.

i.e.

{
    int num = 2;
}
num++; // error: out of scope!

Also, you can just ternary it:

const unsigned char *packet = (mode == 0) ? read_serial_packet(src, &len) : read_network_packet(fd, &len);
Joshua
As mnemosyn answered, yeah, the declaration was out of the scope, I didn't know that.
echedey lorenzo