944,066 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 2981
  • C RSS
Mar 29th, 2005
0

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

Expand Post »
A teacher of mine at NCC course said that a Array cell contains the memory address (pointer) of the next cell. is it true ?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hasan2003 is offline Offline
8 posts
since Mar 2005
Mar 29th, 2005
0

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

No. It contains whatever data the array type is.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Mar 29th, 2005
0

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

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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 29th, 2005
0

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

Quote originally posted by Narue ...
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++;
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hasan2003 is offline Offline
8 posts
since Mar 2005
Mar 29th, 2005
0

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

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 29th, 2005
0

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

Looking forward to this.
Reputation Points: 113
Solved Threads: 3
Posting Whiz
Asif_NSU is offline Offline
353 posts
since Apr 2004
Mar 30th, 2005
0

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

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hasan2003 is offline Offline
8 posts
since Mar 2005
Mar 30th, 2005
0

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

>"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]
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 30th, 2005
0

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

LOL!

Quote ...
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!
Reputation Points: 12
Solved Threads: 5
Posting Pro
Acidburn is offline Offline
510 posts
since Dec 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Help with a 2D Matrix problem
Next Thread in C Forum Timeline: Using a variable as an array's index?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC