so whats the deal with pointers and assignment?

Started by
25 comments, last by TwistedMatrix 21 years, 4 months ago
quote:
Pointers and arrays are virtually the same thing.

You could simply use the sizeof operator to prove it wrong.
"after many years of singularity, i'm still searching on the event horizon"
Advertisement
Haha yeah, i was typing that up right when you posted
I don''t know if this will just add to the noise, but...

Pointers and arrays are not the same. If you have an array, you have a row of variables in memory, no pointer is involved. If you have a pointer, it''s like an address in memory.

The compiler can convert from an array to a pointer by using the address of the first element. The [] operator is defined for pointers to make it behave like it does for an array.

Arrays are always passed by reference into a function, meaning that the whole array isn''t passed in. If you modify the array inside the function, the caller''s array gets modified.

Hope this helps somehow!

Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions
Hey, I figured out my problem. It wasnt actually in that routine, it was in the move_lemming() routine.

I have another important question. Why is it that either of the folloing work. whats the difference? And why can you not nest the routines?
struct stuff  {    int a;    char b;  };// only needs one elementvoid funct1(struct stuff *elem){  printf("a=%d  ",elem.a);  printf("b=%d\n",elem.b);}// needs multiple elementsvoid funct2(struct stuff *elem){  int i;  for (i=0; i< 5; i++) {    printf("a=%d  ",elem->a);    printf("b=%d\n",elem->b);<br>  }<br>}<br><br>// combination of the two (dosent work)<br>void funct2(struct stuff *elem)<br>{<br>  int i;<br>  for (i=0; i< 5; i++) {<br>    funct1(&test); <br>  }<br>}<br><br>int main()<br>{<br>   struct stuff test[5];<br>   funct1(&test[0]);<br>   funct2(test);<br>   funct3(test);<br>   return 0;<br>}<br>  </pre>  <br><br>This was just typed out of my head wo it *may* caontain an error. If so I appologize.<br><br>   </i>    <br><br>- Twisted Matrix<br><br><SPAN CLASS=editedby>[edited by - TwistedMatrix &#111;n November 21, 2002 9:46:05 AM]</SPAN>    
- Twisted Matrix
quote:Original post by TwistedMatrix
I have another important question. Why is it that either of the folloing work.

They don''t. You have your member access operators the wrong way around. The code should look like this:


  #include <stdio.h>typedef struct STUFF  // use typedef to enter stuff into the symbol table{	int a;	char b;} stuff;void funct1(stuff *elem){	printf("a=%d  ",elem->a);	printf("b=%d\n",elem->b);}void funct2(stuff *elem){	int i;	for (i=0; i< 5; i++)	{		printf("a=%d  ",elem[i].a);		printf("b=%d\n",elem[i].b);	}}void funct3(stuff *elem){	int i;	for (i=0; i< 5; i++)	{		funct1(&elem[i]);	}}int main(){   stuff test[5];   funct1(&test[0]);   funct2(test);   funct3(test);}  

quote:
whats the difference?

All functions have the same signature, and all expect a pointer to "stuff". The difference comes about in what assumptions you make about the the memory layout at "elem". For instance, in funct1(), you assume that you have a valid pointer to a "stuff" entity. In funct2(), you assume that you have a valid pointer to the beginning of an array of 5 "stuff" entities. In funct3(), you also assume that you have a valid pointer to 5 items, and then forward each of those pointers on to funct1(). The problems come when your assumptions breakdown, which is highly likely in a program of above-trivial complexity. If you want to pass a pointer to an array, you should at least pass an additional field stating how many elements the array passes. This is C bogosity at its best.
quote:
And why can you not nest the routines?

You mean why can''t you delegate from funct3() to funct1()? You can if you write your code correctly.
thanks.
BTW: you have a nice coding style. very readable.
- Twisted Matrix
I think what an earlier poster meant by ''arrays and pointers are basically the same thing'' was that the tokens for arrays and pointers act in exactly the same way.

If I declare the array int foo[4], then the expression foo has type int*. It is essentially a pointer to a block of memory which is sizeof(int)*4 bytes long.

Rather than allocating an array like that,

struct structure_one
{
char var1;
int var2;
}; //structure_one has size 3 (1+2)

struct array_structure
{
char data[12];
}; //array_structure has size 12

array_structure s;
structure_one *a=(structure_one*)&s
a[0].var1=someVal;
...


When I create ''s'' on the stack, it''s a blob of memory which is 12 bytes long. I can choose to address that memory as if it were an array_structure (where ''data'' starts at position 0), or I can address it as if it were an array of 4 structure_ones (where ''var1'' starts at positions 0, 3, 6, 9, and ''var2'' at 1, 4, 7, and 10, for each element of that array).

Please note: the above post contained extremely bad programming techniques and should not be used in any life-critical situations.

Superpig
- saving pigs from untimely fates
- sleeps in a ham-mock at www.thebinaryrefinery.cjb.net

Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse

This topic is closed to new replies.

Advertisement