Thread: Cant understand the outout

  1. #1
    Registered User
    Join Date
    Jun 2011
    Location
    Bolpur, India
    Posts
    42

    Cant understand the outout

    A simple program is:
    Code:
    //Array Access
    #include<stdio.h>
    void main()
    {
       int n[3][3]={
                     2,4,3,
                     6,8,5,
                     3,5,1
                    };
     int i,*ptr;
     ptr=&n;
     for(i=0;i<9;i++)
     {
       printf("%d",*(ptr+i));     //should print 2,6 and 3 as n[0],n[1] and n[2]
     }
    }
    Now my understanding is a 2 dimensional array is nothing but a collection of one dimensional array within another one dimensional array.So when i am pointing n,the address is held in ptr like n[0][0]..
    and *(ptr+i)=ptr[i]=i[ptr]
    So ptr[i] should=ptr[0],ptr[1],etc which is invalid as it is not pointing to any particular element like ptr[0][1] or ptr[1][2].So how the output is printed?
    Help me in correcting the concept!!
    I also know we can access each element of such array if we have already initialised a pointer to an array.But here nothig is done like that!!

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    First of all it's int main(void) and it returns 0, not void main().

    Secondly, ptr is a pointer to an int, which means that it holds the memory address of an integer. n is not an integer. Had you done something like ptr = &n[1][1] that would have been correct.

    I suggest you read the tutorials on this site on pointers and arrays before proceeding further.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Location
    Bolpur, India
    Posts
    42
    no,I didnot use &n[1][1].....Because just writing n, means i am giving the address n[0][0] in case of array.

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by mistu4u View Post
    no,I didnot use &n[1][1].....Because just writing n, means i am giving the address n[0][0] in case of array.
    And what type is that? (hint: it's not int*)
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by mistu4u
    So ptr[i] should=ptr[0],ptr[1],etc which is invalid as it is not pointing to any particular element like ptr[0][1] or ptr[1][2].So how the output is printed?
    As claudiu reminded you, ptr is a pointer to an int, not a pointer to an array. What you did was to make it point to the same address as n[0][0]. The elements of n are contiguous, and the elements of each element of n is also contiguous, thus you can iterate over all the ints in n through ptr just by incrementing ptr.

    Quote Originally Posted by mistu4u
    no,I didnot use &n[1][1].....Because just writing n, means i am giving the address n[0][0] in case of array.
    You understand that the address of an array and the address of its first element are the same in value, but you don't understand type. This lack of understanding is the root of the problem.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jun 2011
    Location
    Bolpur, India
    Posts
    42
    Thank yo guys for making your points clear......

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. please help me understand this
    By litzkrieg in forum C Programming
    Replies: 8
    Last Post: 02-16-2011, 01:44 PM
  2. Don't Understand
    By Trckst3 in forum C++ Programming
    Replies: 9
    Last Post: 04-04-2008, 11:58 PM
  3. -1.#J outout of sprintf
    By vart in forum C Programming
    Replies: 13
    Last Post: 03-03-2008, 01:26 PM
  4. I don't understand what this is asking!
    By mabufo in forum C++ Programming
    Replies: 10
    Last Post: 02-05-2006, 10:24 AM
  5. help me understand...
    By stormfront in forum C Programming
    Replies: 2
    Last Post: 10-12-2005, 10:47 AM