446,332 Members | 1,505 Online
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,332 IT Pros & Developers. It's quick & easy.

# A beginner's problem

 P: n/a I got from a book a 2D array(a[i][j] is resolved by compiler as a[i][j]=*(&a[0][0]+total_columns*i+j); // ->1 [Stephen Holzners Assembly Language with C] And I know when I use pointer to array I should refer a[i][j]=*(*(a+i)+j); ->2 Is this also resolved by the compiler to (->1) (Awkward question I know) And my main question is why we use *(a+i) here In the pointer to array notation a[i][j]=*(a[i]+j); & this again resolves to a[i][j]=*(*(a+i)+j); which is same as (2) Then whats the need of both array of pointers and pointer to array Is this makes compilers work easy Dec 27 '05 #1
7 Replies

 P: n/a "smartbeginner" writes: I got from a book a 2D array(a[i][j] is resolved by compiler as a[i][j]=*(&a[0][0]+total_columns*i+j); // ->1 [Stephen Holzners Assembly Language with C] And I know when I use pointer to array I should refer a[i][j]=*(*(a+i)+j); ->2 Is this also resolved by the compiler to (->1) (Awkward question I know) And my main question is why we use *(a+i) here In the pointer to array notation a[i][j]=*(a[i]+j); & this again resolves to a[i][j]=*(*(a+i)+j); which is same as (2) Then whats the need of both array of pointers and pointer to array Is this makes compilers work easy Have you read section 6 of the comp.lang.c FAQ ()? If not, please do so, then feel free to come back with any questions you still have. -- Keith Thompson (The_Other_Keith) ks***@mib.org San Diego Supercomputer Center <*> We must do something. This is something. Therefore, we must do this. Dec 27 '05 #2

 P: n/a I'm a student,i want to ask how to learn the c language. Thank you very much! Dec 27 '05 #3

 P: n/a tz*****@gmail.com said: I'm a student,i want to ask how to learn the c language. Thank you very much! If you have prior programming experience in some other language, I recommend that you start by buying a copy of: The C Programming Language, 2nd Ed. Kernighan & Ritchie. Prentice Hall, 1988. ISBN 0-13-110362-8 (paperback), or 0-13-110370-9 (hardback). If you have no previous experience, I instead suggest one of these two books: C Programming: A Modern Approach, K.N.King, W.W.Norton & Company, 1996. ISBN 0-393-96945-2 C: How to Program, 2nd Ed. Deitel, H.M. & Deitel, P.J. Prentice Hall, 1994. ISBN: 0-13-226119-7 -- Richard Heathfield "Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk email: rjh at above domain (but drop the www, obviously) Dec 27 '05 #4

 P: n/a wrote I'm a student,i want to ask how to learn the c language. Thank you very much! Create an ASCII file with the contents #include int main(void) { printf("Hello world\n"); return 0; } Now get a C compiler. Fiddle with the compiler until the program compiles and links. Now modify the program to print out "Goodbye cruel world". Then modify the program agian to do something else. Continue modifying the program until you can make it do whatever you want. Dec 27 '05 #5

 P: n/a smartbeginner wrote: I got from a book a 2D array(a[i][j] is resolved by compiler as a[i][j]=*(&a[0][0]+total_columns*i+j); // ->1 [Stephen Holzners Assembly Language with C] And I know when I use pointer to array I should refer a[i][j]=*(*(a+i)+j); ->2 Is this also resolved by the compiler to (->1) (Awkward question I know) And my main question is why we use *(a+i) here In the pointer to array notation a[i][j]=*(a[i]+j); & this again resolves to a[i][j]=*(*(a+i)+j); which is same as (2) Then whats the need of both array of pointers and pointer to array Is this makes compilers work easy Unless some C standard or other has changed things, rely on the following: x[5] is the same as *(x+5) x[5][4] is the same as *(*(x+5)+4) x[5][4][6] is the same as *(*(*(x+5)+4)+6) and so on... The multiplications are taken care of because adding an integer to a pointer causes the integer to be scaled by the size of what is pointed to. My whole life is pretty much based on this... ;-) -- +----------------------------------------------------------------+ | Charles and Francis Richmond richmond at plano dot net | +----------------------------------------------------------------+ Dec 27 '05 #6

 P: n/a Malcolm wrote: wroteI'm a student,i want to ask how to learn the c language.Thank you very much! Create an ASCII file with the contents #include int main(void) { printf("Hello world\n"); return 0; } Now get a C compiler. Fiddle with the compiler until the program compiles and links. Now modify the program to print out "Goodbye cruel world". Then modify the program agian to do something else. Continue modifying the program until you can make it do whatever you want. That's pretty good advice. I still do a lot of that. Of course now I call it Software Engineering and Code Re-use. :-) -- Joe Wright "Everything should be made as simple as possible, but not simpler." --- Albert Einstein --- Dec 27 '05 #7