#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.
char tno[9]you have a "string" for eight characters. Remember thatcharstrings in C++ are really called null-terminated byte strings. Don't forget space for the terminator, or you will go out of bounds and have undefined behavior.p? The expression(p + i)->nameis equal top[i].namewhich of course is the same asa[i].name.std::stringinstead of c-strings, andcoutinstead ofputs. Smart pointers instead of raw pointers. And also consider switching to some new IDE.include<iostream.h>use a compiler from this century.