Compound07
Compound07

Reputation: 143

How to differentiate empty and 0 in an int array in C?

I'm a beginner of C and now I'm learning pointer and dynamic memory allocation. I want to write a simple program to create empty arrays and check for the existence of a given number. Here's my code:

/* create an empty array pointer */
int* createArray(){
    int *a = (int*) malloc(sizeof(int));
    return a;
}

void findArrayElement(int *list, int element){

    int i;
    int len = (sizeof(list) / sizeof(int));
    if (sizeof(list) == 0) {
        printf("NO\n");
        return;
    }
    for (i=0; i<len; i++) {
        if (list[i] == element) {
            printf("YES\n");
            return;
        }
    }

    printf("NO\n");
}

int main(int argc, const char * argv[]) {

    int *p;
    p = createArray();

    printf("size of int is: %lu\n", sizeof(int));
    printf("size of p is: %lu\n", sizeof(p));
    printf("LENGTH of p is: %lu\n", ARRLENGTH(p));

    findArrayElement(p, 2);
    findArrayElement(p, 0);

    return 0;
}

But when I run the program, I always get 'YES' when I looking for 0, so

  1. Is there a way to differentiate integer 0 and a complete empty array?
  2. Also I'm not sure whether my function createArray() is a correct way to create an empty array.

Thanks guys.

Upvotes: 2

Views: 5096

Answers (2)

Alex Lop.
Alex Lop.

Reputation: 6875

Is there a way to differentiate integer 0 and a complete empty array?

How do you define an empty array? Once you allocate a memory chunk and assign it to a pointer, it already has some value (which is undefined in case of alloc). The most used way to mark a pointer as not used or not allocated os to assign NULL to it.

Also I'm not sure whether my function createArray() is a correct way to create an empty array.

sizeof returns the number of bytes which the given object (or type) occupies in the memory. In your case sizeof(list) returns 8 as it is a pointer. In oder to allocate an array, the function has to receive its size. Currently it always allocates size for one integer only.

Edit: Adding example.

/* create an empty array pointer */
int* createArray(size_t size)
{
    return (size ? (int*) malloc(sizeof(int)*size) : NULL);
}

So now the returned pointer should be 'coupled' with the size of the array. Which means that each function that receives an array as a parameter should receive also its size.

Upvotes: 5

jwkicklighter
jwkicklighter

Reputation: 126

sizeof returns the memory size of the array pointer, regardless of contents.

edit: if it exists in memory, it will be nonzero.

edit 3: removed inaccurate information, see the comments about creating a variable to record the length. Also from comments, note that your createArray function is creating an array for exactly 1 integer. In C, arrays are of fixed length. So this Array will always be the same size (whether you stored something in it or not). sizeof(pointer) will always return the memory allocated for the pointer, not the memory allocated for the array at which it is pointing.

Upvotes: 0

Related Questions