Linked List Problem

Hello all,
I'm trying to implement a linked list and enter data respectively. I've written a quite simple code for this. In the codes, I defined a struct (the node of the list) where the number and name of each will be held. The problem is, as starts at 23rd line here, in the for loop. I want the loop to iterate as i times the user entered, and ask for name & number at each. But that part doesn't work properly. Could you help me please, thanks in advance.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

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

#define MAX_LENGTH 20
#define MAX_NUMBER 13

using namespace std;

struct phonenode{
   char name[MAX_LENGTH];
   char number[MAX_NUMBER];
   phonenode *next;
   } *p;

void enter_records(){
     int i, k;
     phonenode *q;
     cout << "How many records will you enter?" << endl;
     cin  >> i;
     cout << "Enter them" << endl;
     q=p;
     for(k=1; k<=i; k++){
              q = new phonenode;
              cout<< "Name:";
              cin >>  q -> name[MAX_LENGTH];
              cout <<endl;
              cout<< "Number:";
              cin >>  q->number[MAX_NUMBER];
              cout << endl;
              q = q -> next;
     };
}


int main(){
    enter_records();
    return 0;
}
Seems like I've fixed the problem without knowing why I've done that :S I replaced
q -> name[MAX_LENGTH] with only q ->name and same for the number. So why it didn't work while it had the [MAX_NUMBER] part?
MAX_NUMBER and MAX_LENGTH are macros that translate into numbers when you compile your program. So by having that written inside of the brackets you were telling the system that you wanted that data at the end of the array.
Actually, data 1 element past the end of the array.

name[MAX_LENGTH] only has elems 0, 1, ..., MAX_LENGTH-1

there is no elem MAX_LENGTH.

When you use operator[] with an array, you are accessing an individual elem. With a string in a char array, that mean's you get just one char.
Topic archived. No new replies allowed.