954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Array cell contains the memory address of the next cell ????

A teacher of mine at NCC course said that a Array cell contains the memory address (pointer) of the next cell. is it true ?

hasan2003
Newbie Poster
8 posts since Mar 2005
Reputation Points: 10
Solved Threads: 0
 

No. It contains whatever data the array type is.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

Your teacher is confusing arrays with linked lists. An array is always a contiguous sequence of cells, so there's no need for one cell to "point" to the next. Because the nodes of a linked list are not contiguous in memory, a pointer to the next node is critical.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 
Your teacher is confusing arrays with linked lists. An array is always a contiguous sequence of cells, so there's no need for one cell to "point" to the next. Because the nodes of a linked list are not contiguous in memory, a pointer to the next node is critical.

That’s what I told him. But he told me that I am getting confused :rolleyes: . He said that the array and link list follows the same principle. And each cell contains the next cell address. I told him that they don’t. It is true only for link list not array. Till this it is all right for me. But, then what is the internal mechanism of x[i] ?

Is it something like this ….

int *p;
int x[]={1,2,3,4};

p=&x[0];

// to get the next index

p++;

hasan2003
Newbie Poster
8 posts since Mar 2005
Reputation Points: 10
Solved Threads: 0
 

>He said that the array and link list follows the same principle.
Then he's trying to overgeneralize and comes to the wrong conclusion. Linked lists must point to the next item because there's no other way to find it. Arrays are always contiguous, so moving to the next item in an array of type T is as simple as jumping T bytes forward from the current item. No link is necessary, it's all about offsets, and the offsets can be calculated ad hoc.

>But, then what is the internal mechanism of x[i] ?
x[i] is converted to *(x + i), which eventually becomes *(x + (i * sizeof *x)).

To avoid making your teacher look like a total idiot, it is possible to create linked lists using arrays, where a structure of two items is stored in each cell. One item contains the data and the other contains either the index or the address of the next cell in the list. However, arrays do not work like linked lists internally.

Ask him to prove it. The only way he'll be able to do so is to show you code that doesn't do what he thinks it does, show you code that does what he thinks it does but not what he says it does, or quote another incorrect source. If anything, it should be entertaining to see his reasoning. Please post it so we can all get a good laugh. :)

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

Looking forward to this.

Asif_NSU
Posting Whiz
353 posts since Apr 2004
Reputation Points: 113
Solved Threads: 3
 

I told him to prove it. But instead he said “do u doubt that I will teach u something wrong? I don’t know what to say after this. So I just left it. I don’t have any wish to fight him. Cause my project mark is on his hand. Thanks for the help any way. I was really confused after what he said about array.

Thanks

hasan2003
Newbie Poster
8 posts since Mar 2005
Reputation Points: 10
Solved Threads: 0
 

>"do u doubt that I will teach u something wrong?"
Hell, yes! Any teacher who's so arrogant as to think he's infallible is not someone you want to be learning from. I recommend finishing out the class (always double checking everything he teaches with a trusted source) and then finding a new instructor. It's obvious from that answer that your teacher thinks he knows everything, and is unwilling to double check himself for accuracy. The good news is that you can probably get away with turning in really crappy code because he doesn't know the language as well as he believes. ;)

[edit]
Please invite him here. I'd love to meet this guy, he sounds like another teacher that I put in his place a couple of years ago.
[/edit]

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

LOL!

Please invite him here. I'd love to meet this guy, he sounds like another teacher that I put in his place a couple of years ago.

I wish i was there when you did it!

Acidburn
Posting Pro
511 posts since Dec 2004
Reputation Points: 12
Solved Threads: 5
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You