lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


It was thus said that the Great Paige DePol once stated:
> Sean Conner <sean@conman.org> wrote:
> > 
> > 	int  big_array[1024];
> > 	int  single_int;
> > 	int *pa;
> > 	int *pb;
> > 	int *pc;
> > 	int  a;
> > 	int  b;
> > 	int  c; 
> > 	int  d;
> > 	int  e;
> > 
> > 	pa = big_array; // okay, big_array referenced here becomes a pointer
> > 	pb = &big_array[0]; // the same as the above line
> > 	pc = single_int; // error---single_int here just returns a value when a pointer is expected
> > 	pc = &single_int; // this is fine
> > 
> > 	a = *pa;	// okay
> > 	a = *big_array; // error---big_array is an array, not a pointer
> > 	b = pa[0];	// same as "a = *pa"
> > 	c = *pc;	// also okay
> > 	c = single_int; // still okay
> > 	d = pc[0];	// again okay
> > 	e = pa[2];	// okay, because pa points to big_array and index 2 exists
> > 	e = pc[2];	// undefined bahavior, because pc points to
> > 			// single_int, which is *NOT* an array.
> > 
> > I'm not even going to go into the whole 2[pa] bit here ... but I hope I got
> > across that pointers and arrays are not exactly the same thing.
> 
> I created some sample code using the following files: [1]
> 
> ===== ack.h =====
> extern int big_array[];   // Size not required here
> 
> ===== ack.c =====
> #include "ack.h"
> int big_array[5] = { 5, 4, 3, 2, 1 };  // big_array now a pointer
> 
> ===== oop.c =====
> #include <stdio.h>
> #include "ack.h"
> 
> int main() {
> printf("a[%p] b[%p] c[%p]\n", big_array, &big_array[0], &(*big_array));
> printf("a[%p] b[%p] c[%p]\n", big_array + 3, &big_array[3], &(*(big_array + 3))); 
> a = *big_array; // not an error is same as big_array[0]
> b = *(big_array + 2);  // ...and big_array[2]
> c = *(big_array + 4);  // ...and big_array[4]
> printf("a[%d]  b[%d]  c[%d]\n", a, b, c);
> printf("a[%d]  b[%d]  c[%d]\n", big_array[0], big_array[2], big_array[4]);
> }
> 
> Compiling the above examples using 'cc oop.c ack.c -Wpedantic" generates no
> errors or warnings, other than printf expecting void*, and generates this:
> (obviously the pointer values will change between successive executions)
> 
> a[0x101193020]  b[0x101193020]  c[0x101193020]
> a[0x10119302c]  b[0x10119302c]  c[0x10119302c]
> a[5]  b[3]  c[1]
> a[5]  b[3]  c[1]
> 
> Other than generating an error if you try to implement a static array into a
> variable that was declared as a pointer, arrays themselves are pointers and
> the [] notation is just syntactical sugar for pointer arithmetic... I mean
> unless I am missing something really fundamental here about your examples?

=======[ a.c ]==========

int big_array[1024] = { 1 , -1 };

=======[ m.c ]==========

#include <stdio.h>

extern int *big_array;

int main(void)
{
  printf("%d %d\n",big_array[0],big_array[1]);
  return 0;
}

[spc]lucy:/tmp>gcc -Wall -Wextra -pedantic a.c m.c
[spc]lucy:/tmp>./m 
Segmentation fault (core dumped)
[spc]lucy:/tmp>

  There's a mismatch between the big_array instance and the big_array
declaration.  

> > c = single_int; // still okay
> 
> This example generates a warning, however, as you are trying to assign an int
> value to a pointer variable, which is probably not what you want to do.

  Look again at my original code---both single_int and c are plain integers. 
Also, in your code, where do you declare a, b, and c?  I don't see those.

> I don't know what you mean by "the whole 2[pa] bit" though, can you explain?

#include <stdio.h>

char *a[] = { "zero" , "one" , "two" , "three" };

int main(void)
{
  puts( a[0] );
  puts( 1[a] );
 
  putchar( 2[ 3[a] ] ); putchar('\n');
  putchar( 2["0123"] ); putchar('\n');

  return 0;
}

  Valid.  And it runs without error.

  -spc (And you should never never do that!)