Thread: Help for understand pointer

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    6

    Help for understand pointer

    Hello,

    I'M a newbie C programming,i read book to learn C,in chapter of pointers and arrays, written an example that i write :

    Code:
    #include <stdio.h>
     
    void main()
    {
    	char test[3][3] = {{4,5,1},{6,9,7},{3,2,8}};
    	for(int i = 0;i<9;i++){
    			printf("%d\n",*(*test + i));
    	}
    }
    output this code is all of tests elements.

    in the line printf("%d\n",*(*test + i)); , in section (*test + i) in section c in each loop i addition to address of first byte of test array.t type is char and each element have 1 byte,thus when address of first byte addition to i,in each loop we go to the next element of array.i changed type of test to int,and compiled this sample again, when type of test is int each element have 4 bytes,thus when *test + i we shouldn't go to next element,but when i printf address of *test + i, i saw the address grows 4 bytes !!
    why after i change type of test,when i addition i to *test, address grows 4 bytes ?
    i can't understand what happened

    excuse me for my bad english .
    thanks in advance .

  2. #2
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    Why are you defining test as an array of 3 arrays of 3 chars when you're accessing it as if it's an array of 9 chars? Your problem is a misunderstanding of types.

    in section (*test + i) in section c in each loop i addition to address of first byte of test array.
    No, you're accessing i chars from test[0]. Think about the types of each expression.

    test + 0: char (*)[3]
    test[0] + 0, *(test + 0) + 0: char *
    test[0][0], *(*(test + 0) + 0): char

    The second one is the one that applies here. You're pointing *into* test[0] which is exactly three chars wide. Dereferencing test[0][3], or anything beyond that, is incorrect.

    t type is char and each element have 1 byte,thus when address of first byte addition to i,in each loop we go to the next element of array.
    Why do you assume that the first element of the second array is the next element after the last element of the first array? They are separate arrays.

    why after i change type of test,when i addition i to *test, address grows 4 bytes ?
    Isn't it obvious? sizeof (int) happens to be four bytes on your C implementation, and sizeof (char) is always one. test[0] + 1 adds (sizeof (int)) to test[0] when you define test as an int[3][3]. If you were to do test + 1, it would be scaled by (sizeof (int[3])).

    I suggest fixing your code so that it's correct. You could either change the type of test to “T test[9]” and dereference each element with either “*(test + i)” or “test[i]”, or keep the type you have and use two indices, like so:

    Code:
    int i, j;
    
    for (j = 0; j < 3; j++)
            for (i = 0; i < 3; i++)
                    printf("%d\n", test[j][i]);
    Otherwise your code is simply incorrect.

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Well, never forget that main usually returns an integer like this,
    Code:
    int main(void)
    {
          ...
          return 0;
    }
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  4. #4
    Registered User
    Join Date
    Jan 2013
    Posts
    6
    Thanks to replies, i can't understand properly pointers and types, what is you're suggestions to learn properly pointers and types ?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If your "book" is teaching you the code you posted in post #1, then you need a better book.

    Whilst flattening a 2D array into a 1D array is something that you can do, it's not particularly vital to know as the first thing you need to know about pointers.

    Try this instead.
    A Tutorial on Pointers and Arrays in C
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6

  7. #7
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    Quote Originally Posted by Salem View Post
    Whilst flattening a 2D array into a 1D array is something that you can do
    If you're referring to the way he's accessing the char[3][3], it yields undefined behaviour. Check section 2 of annex J.

    An array subscript is out of range, even if an object is apparently accessible with the given subscript (as in the lvalue expression a[1][7] given the declaration int a[4][5])
    I suppose it may be feasible to memcpy the data into a char[9], but you can only dereference three pointers from the offset of test[0].

  8. #8
    Registered User
    Join Date
    Jan 2013
    Posts
    6
    What's your suggestion for c programming book,and a reference for learning types in c ??

  9. #9
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    The holy book of C is the one of K&R.

    There is relevant thread about what you ask. Might be interested to you

    BUT, don't forget, programming is mostly taught by actually programming(writing and debugging code yourself) and not by reading just some books . A book can however give a basis .
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Don't understand backtrace on invalid pointer
    By SterlingM in forum C++ Programming
    Replies: 5
    Last Post: 09-21-2011, 02:00 PM
  2. Can someone help me understand this plz?
    By Arex Bawrin in forum C Programming
    Replies: 5
    Last Post: 02-18-2010, 06:42 PM
  3. Replies: 3
    Last Post: 10-30-2009, 04:41 PM
  4. Replies: 4
    Last Post: 08-27-2007, 11:51 PM
  5. I don't understand K&R example
    By rllovera in forum C Programming
    Replies: 8
    Last Post: 10-25-2004, 10:45 AM