English 中文(简体)
C. 如果发言,不得在内部宣布点名
原标题:C: Cannot declare pointer inside if statement

我有一点要说明一项职能。 我谨:

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

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

但我不能这样做,因为我的汇编者在我先在守则稍后使用点时抱怨。

   error:  packet  undeclared (first use in this function)

这是奇怪的。 它没有发言,但现在我需要我的方案才能从不同来源获得数据。 难道不可能这样做吗? 我认为如此。 如果有的话,是否有其他简单的方式来获取我正在尝试的东西?

感谢很多。

最佳回答

您需要审查blocks或compound-statements的概念或范围。

如果您在复合说明中宣布变数({>,>>><>>>>>>/code>,则该变数仅在该确切范围上宣布。

因此,修改你的法典

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. 
问题回答

任何种类的括号都将排除其范围,即使不是像、但还是这样。

i.e.

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

此外,你也可以说是永恒的:

const unsigned char *packet = (mode == 0) ? read_serial_packet(src, &len) : read_network_packet(fd, &len);

在一个区块中申报的变数在外面看到。

  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

页: 1 。 根据您的条件:

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

这是因为,曲线的薄膜({>}使范围更广。 这意味着,这些图书馆之间界定的每个变量都是在范围范围内界定的,因此在范围外看不到。

为了使其发挥作用,你需要界定<条码>。 此前:

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

如果发言,你就可以在发言中宣布变数,然后使用这些变量,因为简单的原因是,通过该方案,某些途径不存在记忆地点。

EDIT: 更早宣布。

当你确定变量时,你只能在规定范围内使用。

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

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

因此,你需要把定义移到你所需要的最大范围:

{
    const unsigned char *packet;

    {
        packet = read_serial_packet(src, &len);

    }

    // packet still in scope here

}

// and packet is no longer in scope here 

2. 在发言前宣布:

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

} else {
    packet = read_network_packet(fd, &len);
}

如果声明条款的范围在声明的结尾处,则该名仲裁员的宣布即行。 如果发言,你应在外面宣布点名,并在外派。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...