-1

I'm learning programming for the first time and start with C and work with a German programming book. Unfortunately, the pages in the dynamic memory management section are a bit short and explained quickly, which is why I don't get along very well.

How big is my array?

The size of an Int, but I define array[0] and array[1], how far can I go?

I tried Python briefly back then and there I knew arrays like something like my_Array[1][2] etc and accordingly I could output each thing individually and understood where it was. Here, unfortunately, it does not go into my head at the moment.

Now if I put in array[1] the number 3, then the number 4.

How are the numbers stored here?

Where are they stored?

How can I retrieve every number from array[1]?

If I do realloc(array, array[0]+1), in what way does the 0 field expand from that array?

In my mind, the array was one-dimensional, so that the numbers are lined up as follows:

array[0] = {..,3,4,5,...}

array[1] = {..,3,4,5,...}

And the command always provides only one more place at the end of this array.

I just took the code from the solution now.

Thank you in advance!

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int value;


    int *array = malloc(sizeof(int));

    array[0] = 0;

    int i = 1;


    while (value >= 0)
    {
        printf("Enter a positive number to add a nubmer\n");
        printf("Or enter a negative number to end.\n");

        printf("Enter number: ");

        scanf("%i",&value);

        if (value >= 0)
        {

            array = realloc (array, array[0] + 1);

            array[i] = value;
            i++;
            array[0]++;
        }

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

    }
    free(array);

    return 0;```
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Kai
  • 1
  • 1
  • Does this answer your question? "[How dangerous is it to access an array out of bounds?](/q/15646973/90527)", "[Arrays in C are used through pointers?](/a/4772984/90527)", "[Accessing elements in a static array using pointer(arithmetic) in C](/q/9489396/90527)", "[Is an array name a pointer?](/q/1641957/90527)" – outis Aug 21 '22 at 18:49
  • ... "[Are "array" element values stored in one location or in separate locations?](/q/40364357/90527)" – outis Aug 21 '22 at 19:00

1 Answers1

1

For starters the program has undefined behavior at least because you are using the uninitialized variable value in the condition of the while loop

int value;

//...

while (value >= 0)

You should write for example

int value = 0;

//...

while (value >= 0)

Or instead of the while loop it will be better to use a do-while loop.

Secondly initially the value of array[0] is equal to 0 while the variable i is equal to 1

array[0] = 0;

int i = 1;

So this code snippet

array = realloc (array, array[0] + 1);

array[i] = value;

again invokes undefined behavior because you allocated memory only for one byte. So this statement

array[i] = value;

is trying to access memory beyond the allocated memory.

Pay attention to that the value of the expression used in the call of realloc

array[0] + 1

is equal to 1, 2, 3, ..., n instead of n * sizeof( int ).

And there are no array array[0] nor array array[1]. They are scalar objects of the type int. To access elements of the array use the subscript operator like array[i] where i is less than the value stored in the element array[0]. That is the for loop must look like

for (int i = i; i < array[0]; i++)
                ^^^^^^^^^^^

Also in this call of printf

printf("Value %i: %i\n", array[i]);

there are used two conversion specifiers while only one argument is supplied for them.

The program can look for example the following way

#include <stdio.h>
#include <stdlib.h>

int main( void ) 
{
    int *array = malloc( sizeof( int ) );

    if ( array != NULL )
    {
        array[0] = 1;

        int value;
    
        do
        {
            printf( "Enter a positive number to add a nubmer\n" );
            printf( "Or enter a negative number to end.\n" );

            printf( "Enter number: " );

            if ( scanf( "%d", &value ) == 1 && value >= 0 )
            {
                int *tmp = realloc( array, ( array[0] + 1 ) * sizeof( int ) );

                if ( tmp != NULL )
                {
                    array = tmp;
                    array[array[0]++] = value;
                }
                else
                {
                    value = -1;
                }

                for ( int i = i; i < array[0]; i++ )
                {
                    printf( "Value %i: %i\n", i, array[i] );
                }
            }
            else
            {
                value = -1;
            }
        } while ( value >= 0 );
    }

    free( array );    
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335