A beginner's problem

Discussion in 'C Programming' started by smartbeginner, Dec 27, 2005.

  1. I got from a book a 2D array(a[j] is resolved by compiler as
    a[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[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[j]=*(a+j);
    & this again resolves to
    a[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
     
    smartbeginner, Dec 27, 2005
    #1
    1. Advertising

  2. "smartbeginner" <> writes:
    > I got from a book a 2D array(a[j] is resolved by compiler as
    > a[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[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[j]=*(a+j);
    > & this again resolves to
    > a[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) <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.
     
    Keith Thompson, Dec 27, 2005
    #2
    1. Advertising

  3. smartbeginner

    Guest

    I'm a student,i want to ask how to learn the c language.
    Thank you very much!
     
    , Dec 27, 2005
    #3
  4. 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)
     
    Richard Heathfield, Dec 27, 2005
    #4
  5. smartbeginner

    Malcolm Guest

    <> 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.
     
    Malcolm, Dec 27, 2005
    #5
  6. smartbeginner wrote:
    >
    > I got from a book a 2D array(a[j] is resolved by compiler as
    > a[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[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[j]=*(a+j);
    > & this again resolves to
    > a[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 |
    +----------------------------------------------------------------+
     
    Charles Richmond, Dec 27, 2005
    #6
  7. smartbeginner

    Joe Wright Guest

    Malcolm wrote:
    > <> 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 ---
     
    Joe Wright, Dec 27, 2005
    #7
  8. On 26 Dec 2005 20:34:26 -0800, "smartbeginner" <>
    wrote:

    >I got from a book a 2D array(a[j] is resolved by compiler as
    >a[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[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[j]=*(a+j);
    >& this again resolves to
    >a[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 and p 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[j] is treated as (a)[j], a[j][k] is treated as
    ((a)[j])[k], etc.

    The language standard states that a 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[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>>
     
    Barry Schwarz, Dec 28, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Larry Smith

    Trivial resources problem (beginner)

    Larry Smith, Oct 2, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    526
    Scott Manson
    Oct 2, 2003
  2. =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=

    No Class at ALL!!! beginner/beginner question

    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=, Feb 2, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    622
    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=
    Feb 3, 2005
  3. Rensjuh
    Replies:
    7
    Views:
    1,011
    Mabden
    Sep 2, 2004
  4. Toni
    Replies:
    3
    Views:
    2,263
    msnews.microsoft.com
    Jul 29, 2006
  5. william nelson

    Beginner's Beginner

    william nelson, Apr 11, 2011, in forum: Ruby
    Replies:
    7
    Views:
    236
    7stud --
    Apr 12, 2011
Loading...

Share This Page