473,387 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

A beginner's problem

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 1441
"smartbeginner" <rr******@gmail.com> 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 (<http://c-faq.com/>)?
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 <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 27 '05 #2
I'm a student,i want to ask how to learn the c language.
Thank you very much!

Dec 27 '05 #3
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

<tz*****@gmail.com> 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 <stdio.h>

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
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
Malcolm wrote:
<tz*****@gmail.com> 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 <stdio.h>

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
On 26 Dec 2005 20:34:26 -0800, "smartbeginner" <rr******@gmail.com>
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
You may think you know this but you had it wrong in your previous
message thread.
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


What is the need of integer and float? The answer to both questions
is they do different things and part of your design process is to
choose the object type that works best for your needs and code
accordingly.

There are four key points:

If is a is an array and p is a pointer, then a[i] and p[i] both
designate the i-th object in the collection (assuming it exists).

In most expressions, a term with type array evaluates to the
address of the first element of the array with type pointer to
element.

The [ ] operator associates left to right. This means that
a[i][j] is treated as (a[i])[j], a[i][j][k] is treated as
((a[i])[j])[k], etc.

The language standard states that a[i] is equivalent to *(a+i).
Note that a+i is an expression in pointer arithmetic, not integer
arithmetic, as explained in my response to your previous message
thread. This makes intuitive sense since a+i is the address of the
i-th object and *(a+i) dereferences this address to yield the object
itself, just as described in the first paragraph above.

Chaining these rules together in the correct sequence and applying
them recursively where needed, keeping track of the type of each
expression, will show that a[i][j] always evaluates to *(*(a+i)+j).
This works if a is an array, a pointer to an array, or a pointer to
pointer.
<<Remove the del for email>>
Dec 28 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: jvax | last post by:
Hi all, I hope I'm posting in the right NG... I have a data text file I want to read from a c++ program. the data file goes like this: 90 # number of balls 33 42 13
5
by: Richard B. Kreckel | last post by:
Hi! I was recently asked what book to recommend for a beginner in C++. I am convinced that you needn't study C in depth before learning C++ (though it helps), but cannot find any beginner's...
18
by: mitchellpal | last post by:
Hi guys, am learning c as a beginner language and am finding it rough especially with pointers and data files. What do you think, am i being too pessimistic or thats how it happens for a beginner?...
20
by: weight gain 2000 | last post by:
Hello all! I'm looking for a very good book for an absolute beginner on VB.net or VB 2005 with emphasis on databases. What would you reccommend? Thanks!
10
by: See_Red_Run | last post by:
Hi, I am trying to figure out how to get started with PHP/MySQL. Everything I've read so far says to start with PHP first. I was expecting something like Visual Basic Express or some other type...
4
by: Bails | last post by:
Hi Im an absolute beginner in programming and am using VB.Net Express. To start my larning I decided to do a "Real World" app instead of "hello world" and am creating a Poker Countdown clock. ...
5
by: macca | last post by:
Hi, I'm looking for a good book on PHP design patterns for a OOP beginner - Reccommendations please? Thanks Paul
10
by: Roman Zeilinger | last post by:
Hi I have a beginner question concerning fscanf. First I had a text file which just contained some hex numbers: 0C100012 0C100012 ....
1
by: H. Fraser | last post by:
I've downloaded all of the code and video dealing with the RSS Reader but when i try to run it, it throws an exception, saying 404 page not found. And, As a total beginner, i've no idea where...
22
by: ddg_linux | last post by:
I have been reading about and doing a lot of php code examples from books but now I find myself wanting to do something practical with some of the skills that I have learned. I am a beginner php...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.