Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!uunet!samsung!uakari.primate.wisc.edu!dali!milton!oregon!slewis From: slewis@oregon.uoregon.edu Newsgroups: comp.lang.c Subject: Pointer question Message-ID: <19515.263eadf8@oregon.uoregon.edu> Date: 2 May 90 16:52:56 GMT Distribution: usa Organization: University of Oregon Lines: 42 Hello netters. I have a question about pointers. Well, actually I have a few observations and I'm not sure if they're correct. Maybe you can help... Assume the following variables: int i,j; int **p, *a, *b; Also assume that we have allocated space for a 2D n X m int "array" (p), a n-length linear array (a) and an m-length linear array (b) in the following manner: p = (int **) malloc(n*sizeof(int *)); for(i=0; i < n; i++) p[i] = (int *) malloc(m*sizeof(int)); and a = (int *) malloc(n*sizeof(int)); b = (int *) malloc(m*sizeof(int)); Now, we may use p[i][j], a[i] and b[i] to refer to the elements of these arrays. My understanding is that a[i] is equiv to *(a+i). Now, this implies some strange things, right? First, since (pointer) addition is commutative, a[i] == *(a+i) == i[a]. I've seen this stated before in this newsgroup. But are the following assertions true: 1. p[i][j] == *( *(p+i) + j) == j[p[i]] == j[i[p]] 2. a[b[i]] == *( a + *(b+i)) == b[i][a] == i[b][a] (assuming that b[i] contains a valid [-1 < b[i] < n] number) If so, I think I understand. If not, I'm confused. Scott