0

I'm trying to collect input integers one by one. My array starts with size 1 and I want to expand it by 1, with every input collected (is this a smart thing to do?)

Anyway, this is the code I managed to come up with, but it's not working as intended.

After this process, sizeof(array) always returns 8, which I assume means that the array is only being resized once. (sizeof(int) is 4 bits)

Trying to output the array results in multiple instances of the first input variable.

OUTPUT CODE

for(int s=0;s<sizeof(array)/sizeof(int);s++){
    printf("%i\n",array[i]);
}

ORIGINAL CODE:

    int i;
    int size = 1;
    int *array = malloc(size * sizeof(int));
    int position = 0;

    do{
        i = getchar() - 48;
        f (i != -16 && i != -38 && i != 0) {
            array[position] = i;

            position++;
            size++;
            *array = realloc(array, size * sizeof(int));
        }
    } while (i != 0);

UPDATED STILL NOT WORKING CODE

    int i;
    int size = 1;
    int *array = malloc(size * sizeof(int));
    int position = 0;

    do{
        i = getchar() - 48;
        f (i != -16 && i != -38 && i != 0) {
            array[position] = i;

            position++;
            size++;
            array = realloc(array, size * sizeof(int));
        }
    } while (i != 0);
11
  • 1
    "... but it's not working as intended. What's wrong with it?" - you tell us. Commented Oct 27, 2016 at 20:13
  • What happens is when I loop through the resulting array, all values outputted are equal to the first input value. Additionally, sizeof(array) always returns 8, which means that the array isn't actually getting resized properly? If I start with size 1, and int = 4 bits, that somehow means that the resize is only happening once.. Any ideas? Commented Oct 27, 2016 at 20:40
  • 1
    Please put that explanation in the question. And please do not copy answers back into the question. Rolled back. Commented Oct 27, 2016 at 20:40
  • Updated with explanation of my problem... Commented Oct 27, 2016 at 20:44
  • 1
    You're being extremely unhelpful and pedantic. I've updated my post for you. The change was 1 symbol, the other change was functionally identical code, just more readable. Commented Oct 27, 2016 at 20:50

3 Answers 3

4
array = realloc(...)

not *array. Per the realloc docs, realloc returns the pointer, which you can store directly in your array pointer.

Edit One thing that will make your life easier: use char constants instead of raw numbers. E.g.,

i = getchar();
if(i != ' ' && i != '\n' && i != '0') {
 /*    48-16       48-38        48-0      right? */
    array[position] = i - '0';   /* '0' = 48 */
Sign up to request clarification or add additional context in comments.

Comments

2

One thing that jumps out at me: inside your loop, this line:

*array = realloc(array, size * sizeof(int));

should instead be:

array = realloc(array, size * sizeof(int));

In the original version, you were sticking the result of realloc in the first element of the array by dereferencing the pointer first. Without the asterisk, you're reassigning the array itself.

1 Comment

Thanks for the replies! This was an artefact from copy/pasting, I just tried changing array to array* to see what would happen at first. I corrected this now in my post. What happens is when I loop through the resulting array, all values outputted are equal to the first input value. Additionally, sizeof(array) always returns 8, which means that the array isn't actually getting resized properly? Any ideas? @Jonny Henly
1

(With some copy-paste from my comment:) sizeof(array) returns 8 because it equals sizeof(int*) (array is type int*) which is 8 (you're probably compiling as 64-bit). sizeof doesn't work how you think for pointers to arrays.

Similarly, your output code is wrong, for the same reason. You only print the first two elements because sizeof(array)/sizeof(int) will always be 8/4=2. It should be

for(int s=0;s<size;s++){
    printf("%i\n",array[s]);
}

(note also changed index variable i to s) where size is the variable from your other code chunk(s). You cannot find the length of the array from sizeof if it's dynamically allocated with pointers; that's impossible. Your code must "remember" the size of your array.

2 Comments

For the love of... My error was intuitively using array[i] instead of array[s]... How did I not see that... Anyways thanks, I understand how to use sizeof correctly in this case, though research shows there's more nuances to it
@Aldo You understand how to use sizeof? It doesn't appear so at all. The size 8 comes from the fact that storing a pointer requires 64 bits with your compiler options; it has nothing to do with the number of elements in your array or even the fact that it's an array of ints (make them doubles and see for yourself, still size 8). In fact, your code will probably crash if your array contains only one element, because you always attempt to print exactly two elements (unless you change sizeof(int) to sizeof(double), in which case it would be exactly one element).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.