简体   繁体   中英

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.

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 .

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.

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 . If using character arrays seems to be difficult, maybe you can use 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'

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

turns a[1].name from

"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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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