Thread: Arrays in C

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    1
    Rep Power
    0

    Arrays in C


    #include<stdio.h>

    int main()
    {
    int n;
    printf("Enter a no.: ");
    scanf("%d",&n);

    int x[n];
    printf("Size: %d " , sizeof(x));

    printsize(x);

    return 0;
    }


    void printsize(int x[])
    {
    printf("Size: %ld \n", sizeof(x));
    }

    Output:
    Enter a no: 4
    Size:16 Size:8


    This is my simple code.when i run it, i get two different sizes for array x.Y is it so?
    When I pass x as a param to another function,it returns me a different size!
    Please reply asap.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    159
    Rep Power
    20
    Assuming you're using a C99 compatible compiler I wouldn't be surprised that you got different values for x in main() and x in your function. You're not comparing the same things. In the function you're looking at the sizeof the pointer, not the sizeof the array as in main.

    Jim

IMN logo majestic logo threadwatch logo seochat tools logo