Thread: output for array of pointers is not giving correct values

  1. #1
    kotin
    Join Date
    Oct 2009
    Posts
    132

    output for array of pointers is not giving correct values

    HI ,

    I am giving 1 and 2 inputs for below program.

    output giving some other values as i exptectd.

    I am expecting 1 and 2 as outputs.

    any one can help me ?



    Code:
    int main()
    {
            int *p[2]={};
            int i;
    
            for (i=0;i<2;i++);
            {
                    printf ("enter the nubmer \n");
                    p[i]=malloc(sizeof (int ));
                    scanf ("%d",p[i]);
                    printf ("%d\n",*p[i]);
            }
    
            for (i=0;i<2;i++)
            {
                    printf ("%d\n",*p[i]);
            }
            return 0;
    }
    
    
    
    
    output :
    
    enter the nubmer
    1
    1
    Segmentation fault
    Last edited by nkrao123@gmail.; 09-03-2011 at 07:10 AM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Did you include <stdlib.h> ?

    Try this, instead:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
            int *p[2];
            int i;
    
            for (i=0;i<2;i++)
            {
                    printf ("enter the nubmer \n");
                    p[i]=malloc(sizeof (int ));
                    scanf ("%d",p[i]);
                    //printf ("%d\n",*p[i]);
            }
    
            for (i=0;i<2;i++)
            {
                    printf ("%d\n",*p[i]);
            }
            return 0;
    }
    
    Remove the semi-colon from the end of the first line of the for loop code, and fix the int *p line.
    Last edited by Adak; 09-03-2011 at 07:39 AM.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    What include files are you including?

    When I compile the code as provided I get these error messages:

    main.c||In function ‘main’:|
    main.c|4|error: ISO C forbids empty initializer braces|
    main.c|9|error: implicit declaration of function ‘printf’|
    main.c|9|warning: incompatible implicit declaration of built-in function ‘printf’|
    main.c|10|error: implicit declaration of function ‘malloc’|
    main.c|10|warning: incompatible implicit declaration of built-in function ‘malloc’|
    main.c|11|error: implicit declaration of function ‘scanf’|
    main.c|11|warning: incompatible implicit declaration of built-in function ‘scanf’|
    main.c|17|warning: incompatible implicit declaration of built-in function ‘printf’|
    ||=== Build finished: 4 errors, 4 warnings ===|
    Jim

  4. #4
    kotin
    Join Date
    Oct 2009
    Posts
    132
    Quote Originally Posted by jimblumberg View Post
    What include files are you including?

    When I compile the code as provided I get these error messages:



    Jim
    Hi Jim,

    I am compiling on linux mechine.

    I am not getting any compilation errors.

    hope you can are compiling on windows. I think u need to add
    #include<stdio.h>
    #include<conio.h>
    #include<malloc.h>

    But i am not sure.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Code:
            for (i=0;i<2;i++);  <-- WHOOPS
            {
                    printf ("enter the nubmer \n");
                    p[i]=malloc(sizeof (int ));
                    scanf ("%d",p[i]);
                    printf ("%d\n",*p[i]);
            }
    
            for (i=0;i<2;i++)
            {
                    printf ("%d\n",*p[i]);
            }
    That semi-colon means the if() is not associated with the following block, so i == 2 when it executes. This allocates and assigns p[2], which could cause a seg fault right there, but evidently it by chance does not.

    The seg fault you get happens when the second if() loop starts, because p[0] was never initialized, therefore contains some random address which is dereferenced for a value (but it is out of bounds).

    For malloc() you should:

    Code:
    #include <stdlib.h>
    Last edited by MK27; 09-03-2011 at 07:38 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    kotin
    Join Date
    Oct 2009
    Posts
    132
    HI Adak,

    if i added also i am getting same output.( segmentfault).

  7. #7
    kotin
    Join Date
    Oct 2009
    Posts
    132
    Hi Mk27,

    yes. It my mistake. Now i am getting correct values. Thanqu very much.

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by nkrao123@gmail. View Post
    I am compiling on linux mechine.
    I am not getting any compilation errors.
    You should compile with warnings enabled:

    gcc -Wall

    Which will give you a clue about having not included stdlib.h for malloc().
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I am compiling on linux mechine.

    I am not getting any compilation errors.

    hope you can are compiling on windows. I think u need to add
    #include<stdio.h>
    #include<conio.h>
    #include<malloc.h>

    But i am not sure.
    I am compiling on a Linux machine as well.

    You need to include stdio.h for printf(), scanf() and stdlib.h for malloc().

    You need to compile with at least the -Wall compiler option to emit at least the standard warning messages.

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2011, 03:22 PM
  2. changing array values using pointers
    By dford425 in forum C Programming
    Replies: 8
    Last Post: 01-15-2011, 10:45 PM
  3. structure giving weird output
    By bluetxxth in forum C Programming
    Replies: 7
    Last Post: 02-14-2010, 11:44 PM
  4. Replies: 3
    Last Post: 07-24-2002, 08:46 AM
  5. Replies: 2
    Last Post: 03-07-2002, 10:14 AM