0
#define n 10
int a[n];

I wanted to declare an array globally and modify the size in other function

int main(){ 
  int b;
  printf("Enter the number of elements\n");
  scanf("%d",&b);
  #undef n
  #define n b
  for(int i = 0;i < n; i++)
    scanf("%d",&a[i]);
  display();
}

I modified the size in the main function

void display()
{
  for(int i=0;i<n;i++)
  printf("%d ",a[i]);
}

when i enter a size like 5 and enter the elements 1 2 3 4 5 the output shows 5 elements followed by 5 zeroes 1 2 3 4 5 0 0 0 0 0 how to remove the zeroes?

4
  • 2
    C arrays are not resizable. Use dynamic allocation (aka malloc/calloc/realloc). Commented Feb 11, 2018 at 11:11
  • You could declare a new variable, say size, that keeps track of the number of elements in the array, and when you diplay it, instead of looping from 0 to n, loop from 0 to size. Commented Feb 11, 2018 at 11:14
  • 1
    You have some serious confusion about how #define works... The preprocessor works just by brutal text substitution, so it's not like changing a #define can magically affect a section of text that has already been processed. Commented Feb 11, 2018 at 11:16
  • Though this is marked duplicate I am afraid there are few things not to follow from that answer - casting of malloc is unnecessary. The realloc usage is wrong and it is not a complete example. Commented Feb 11, 2018 at 11:51

2 Answers 2

3

You can't. Once you declare an array you can't change it's size. As an alternative you can emulate this behavior perfectly using a pointer and then allocating dynamically memory and assigning the chunk address to this pointer. You can then realloc memory to incorporate this size change.

It will be something like:-

size_t sz = 10;
int *arr = malloc(sizeof *arr * sz); // sizeof(*arr)*sz (sizeof is operator)
if(!arr){
   perror("malloc");
   exit(1);
}
...
sz/=2; //correcting the size
int *p =realloc(arr,sizeof *arr * sz); 
if(!p){
   perror("realloc");
   exit(1);
}
arr = p;
...
free(arr);

Also macro is not a runtime thing - it is expanded at compile time. And more over you want the array to be resized in run time not compile time. Otherwise you can set the array size to be that size from the very beginning when you wrote the code. That's why macro won't work here.

Note that arr here is not an array - it is a pointer pointing to an allocated memory (the starting address of the memory chunk that was created using malloc). Nothing else.

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

5 Comments

@CookiePLMonster.: I editd both. Check it out
i think it should be free(p) instead of free(arr)
@AmarBal.:Nope...it is not a good way to do so. arr = p is there and it is done via free(arr)
btw, i understood the syntax, i was trying to use define, just checking if it could be used.
@AmarBal.: You know now it can't be used the way you did.... :)
0

What you did was just changing a preprocessor constant. They are evaluated at compile time, so your redefinition is effectively pointless and takes no effect.

Not sure what you're trying to achieve here, since this array is not going to be reallocated. If you only want to limit the amount of entries you are iterating through, ditch your n and replace it with something like:

static const size_t n = 10;
static size_t num_entries = n;

Then instead of redefining n, you should just do

num_entries = b;

and later on use this to iterate through the array. However, do note that this does not reallocate the array so things are going to go terribly wrong if b is bigger than n!

If you want to reallocate the array, you're better off reading up about malloc, realloc and free. As well as some fundamental tutorials about C in the meantime, since basing on your misunderstanding of what preprocessor (#define'd variables) are, you most likely just started with C.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.