0

I created an array and put the value into array as follow

int *ptr_int;
int list_int[10];
int i;
for (i=0; i<10; i++)
    list_int[i] = i + 1;

and I assign a value into list_int array like this

list_int[17] = 18;

When I tried to get the count of array as follow

int size = sizeof(list_int ) / sizeof( int );
printf( "The size of int is %d.\n", size);

the result is only 10.

How could I get the array room count?

5
  • 2
    C arrays don't automagically resize. Commented Oct 8, 2013 at 8:01
  • If I want to add/extend the array, how can I do? Commented Oct 8, 2013 at 8:09
  • @ThuRa: Switch from automatic allocation/freeing to to dynamic allocation/reallocation/freeing. Commented Oct 8, 2013 at 8:12
  • @alk, I'm coming from web. I've a little experience at C. So I'm not clear understand how to do it. Do you have any recommend site to do this? I'm very appreciate to you. Commented Oct 8, 2013 at 8:19
  • 1
    @ThuRa: Read about dynamic memory allocation in general and the system calls malloc()/calloc(), realloc() and free() in particular. Do some tests leaning how to use them and if getting stuck using them come back here to Stackoverflow presenting your findings and issues posing another question and we'll try to help you. Commented Oct 8, 2013 at 8:22

4 Answers 4

6

the result is only 10.

That's the real size. Assigning to list_int[17] is undefined behavior, and it does not magically extend the array.

Sign up to request clarification or add additional context in comments.

Comments

1

You have already defined the max size of your array as 10 with the following definition

int list_int[10];

You can not assign value to list_int[17]. Becacuse list_int[17] is out of the memory of the array list_int[10] that you have defined.

You can only assign value to the element from 0 .. 9

1 Comment

We can assign but we should not because it is Undefined behaviour :).
1
list_int[17] = 18;

This is undefined behavior because the array size is only 10.

Note that with the exception of variable length arrays, sizeof operator is a compile time operator. The result of sizeof(list_int ) and sizeof(int) are determined in compile time.


To implement an array with dynamic size, you need to allocate the array dynamically, you may find realloc pretty helpful.

Comments

1

To Create Dynamic arrays ( allocated at run time )

int n; 

scanf("%d",&n); // read n from the user at run time 

int* x=(int*)malloc(sizeof(int)*n);  // where n is the number of elements you need to allocate

////// after that you can access the array (x) using indexer 

///// reading loop 
for(int i=0;i<n;i++)
scanf("%d",&x[i]);

===============================================================================

Note: if you need more dynamic data structure which can allocate memory for each entry you can use linked lists

Comments

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.