简体   繁体   English

C ++结构的指针数组

[英]C++ pointer array of structure

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>

struct telephone
{
    char name[10];
    char tno[9];
};

void main()
{ 
    telephone a[5];
    clrscr();
    telephone* p;
    p = a;
    strcpy(a[0].name, "Aditya"); // initializing the array
    strcpy(a[1].name, "Harsh");
    strcpy(a[2].name, "Kartik");
    strcpy(a[3].name, "Ayush");
    strcpy(a[4].name, "Shrey");
    strcpy(a[0].tno, "873629595");
    strcpy(a[1].tno, "834683565");
    strcpy(a[2].tno, "474835595");
    strcpy(a[3].tno, "143362465");
    strcpy(a[4].tno, "532453665");

    for (int i = 0; i < 5; i++)
    {  
        puts((p+i)->name);cout<< " ";   //command for output
        puts((p+i)->tno );cout<<endl;
    }
    getch();
}

In this code, while taking output, I am not getting output of names. 在这段代码中,在输出时,我没有得到名称的输出。 I only get output for (p+0)->name and not for anything else, but if I don't initialize the telephone number then I get output of names. 我只会得到(p+0)->name输出,而不会得到其他任何东西,但是如果我不初始化电话号码,那么我会得到name的输出。

A struct is stored in continuous memory locations , so when you assign the tno variable, data higher than its bound size, is attempted to be stored in it, the leftover bits get added to the next memory location . struct存储在连续的内存位置中 ,因此,当您分配tno变量时,尝试将大于其绑定大小的数据存储在其中,剩余的位将添加到下一个内存位置

In your code tno [9] , so it can store max 9 chars , though you give it 9 chars only, but what the strcpy does is that it also adds \\0 to the end, it tries to add it to tno [10] , which does not exist and goes out of bounds and stores it in some other memory location , probably that of the next array, this leads to undefined behaviour. 在你的代码tno [9]所以它可以存储最多 9个字符 ,虽然你给它只有9个字符 ,但什么的strcpy做的是,它也增加了\\0到最后,它试图将其添加到tno [10]它不存在并且超出范围,并将其存储在其他内存位置 (可能是下一个数组的位置)中,这导致未定义的行为。

You just need to change the struct definition as follows: 您只需要按以下方式更改结构定义:

struct telephone
{ 
   char name[10];
   char tno[10]; // if you intend to store 9 digit number
 }; 

Just remember if you intend to store x chars in a character array , then your array must be of size x + 1 . 请记住,如果您打算将x个字符存储在一个字符数组中 ,那么您的数组必须为x + 1 If using character arrays seems to be difficult, maybe you can use std::string 如果使用字符数组似乎很困难,也许可以使用std::string

Telephone number needs to be at least one bigger. 电话号码至少应大一。 The telephone number is overunning by one byte into the next array, and changing the name to '\\0' 电话号码超出一个字节进入下一个数组,并将名称更改为“ \\ 0”

strcpy(a[0].tno ,"873629595" );

turns a[1].name from 将[1] .name从

"Harsh\0"

into 进入

"\0arsh\0"

Structure layout 结构布局

+---+---+---+---+---+---+---+---+---+---+
| A | d | i | t | y | a | \0|   |   |   |
+---+---+---+---+---+---+---+---+---+---+
+---+---+---+---+---+---+---+---+---+
| 8 | 7 | 3 | 6 | 2 | 9 | 5 | 9 | 5 | 
+---+---+---+---+---+---+---+---+---+
+---+---+---+---+---+---+---+---+---+---+
| \0| a | r | s | h | \0|   |   |   |   |
+---+---+---+---+---+---+---+---+---+---+
+---+---+---+---+---+---+---+---+---+
| 8 | 3 | 4 | 6 | 8 | 3 | 5 | 6 | 5 | 
+---+---+---+---+---+---+---+---+---+

None of the telephone numbers has space for the null terminator, and is overwriting beyond its space. 电话号码都没有空终止符的空间,并且超出了其空间覆盖。 This is in fact the next array elements name. 实际上这是下一个数组元素名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM