Like Tree1Likes
  • 1 Post By claudiu

Cant understand the outout

This is a discussion on Cant understand the outout within the C Programming forums, part of the General Programming Boards category; 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; ...

  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*)
    mistu4u likes this.
    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
    20,974
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 12: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, 12:26 PM
  4. I don't understand what this is asking!
    By mabufo in forum C++ Programming
    Replies: 10
    Last Post: 02-05-2006, 09:24 AM
  5. help me understand...
    By stormfront in forum C Programming
    Replies: 2
    Last Post: 10-12-2005, 10:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21