When you declare an array like a[] = {...} the array gets a fixed size which cannot be altered, the size is determined when you compile.

If you want to use dynamic arrays you need to allocate on the heap, in C this is done with malloc and realloc. realloc allows you to resize an array.

e.g.

char* p = malloc(10);
char* q = realloc(p, 5); // now you made the array 5 bytes shorter

you should check the return value of realloc in order to know whether the realloc was successful.

char* q = realloc(p, 5);
if (q != NULL) // successful

ref: https://en.cppreference.com/w/c/memory/realloc

Answer from AndersK on Stack Overflow
๐ŸŒ
LinuxQuestions.org
linuxquestions.org โ€บ questions โ€บ programming-9 โ€บ arrays-overwriting-variables-in-c-271614
Arrays overwriting variables in c
December 29, 2004 - I've spent quite some time on this issue, but I just can't see what I'm doing wrong. Take a look at this: struct test { int array[5]; int value; };
Discussions

How can I overwrite an array of chars (AKA a string), with a new array of chars in C? - Stack Overflow
I want the string in memory to be overwritten with a new string of the same length. I keep getting incompatible types -like errors. ... Your declaration of A is probably off by one. ... @mu is too short: The code in the question is legal C, it's an array of 32 char, initialised with the given ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
overwriting character arrays in C - Apple Community
One of the problems with C is that, ... And yet, so much software, even today, is still written in C. How do programmers do it, you ask? The answer - not well. is there a reason that I should refrain from letting C overwrite the character array like this?... More on discussions.apple.com
๐ŸŒ discussions.apple.com
Overwriting array in C - Stack Overflow
Connect and share knowledge within a single location that is structured and easy to search. Learn more about Teams ... Closed 2 years ago. Why it is a problem to overwrite value in character array in C. More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to overwrite a preallocated char array in C - Stack Overflow
I initialize a char array as a global variable in my C program with a default file location/name as char file[] = "/home/jack/files/data.txt"; Later in the program, if a condition is satisfied I r... More on stackoverflow.com
๐ŸŒ stackoverflow.com
March 7, 2017
๐ŸŒ
Cprogramming
cboard.cprogramming.com โ€บ cplusplus-programming โ€บ 99541-overwriting-array-new-array.html
Overwriting Array with New Array
February 25, 2008 - But then you want to initialise this array with an initialiser list, which implies that you know its length at compile time. I suggest that you use a std::vector<record>, and then have record provide a default value for strings that are not specified.
๐ŸŒ
Apple Community
discussions.apple.com โ€บ thread โ€บ 2193854
overwriting character arrays in C - Apple Community
Your array is 32 chars long but you have to reserve 1 position for the null character indicating the end of the C-string. ... Yeah I know about formatting the number of characters like that and about the terminating null character and all that, but thank you for your post.
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 75071176 โ€บ overwriting-array-in-c
Overwriting array in C - Stack Overflow
If you want to change the array's contents and replace it with "girl", then you should use strcpy: Instead ofstr="girl";, you should write strcpy( str, "girl" );.
๐ŸŒ
DaniWeb
daniweb.com โ€บ programming โ€บ software-development โ€บ threads โ€บ 344695 โ€บ problem-with-c-string-array-overwriting-values
algorithm pseudocode - Problem with C string array overwriting ... | DaniWeb
September 21, 2015 - By the time you print, temp contains only the last token, so all pointers appear to be overwritten. There are a couple more hazards: temp[10] is too small for general input (risking overflow/UB), and the loop that tries to clear temp sets count = 0 before the for, so it never runs.
Find elsewhere
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ changing-array-inside-function-in-c
Changing Array Inside Function in C - GeeksforGeeks
July 23, 2025 - Then if any changes occur in an array in the form of a formal argument inside the function define body then the implemented changes also occur in an array which define in the main(). ... // C Program to change an Array in function #include <stdio.h> // here 'm' is the formal argument void addval(int m[]) { int i; for (i = 0; i < 5; i++) { // adding 10 value to each element // of an array m[i] = m[i] + 10; } } // here 'm' is the formal argument void dis(int m[]) { int i; for (i = 0; i < 5; i++) { printf("%d ", m[i]); } printf("\n"); } void main() { int a[] = { 11, 12, 13, 14, 15 }; printf("Array before function call\n"); // function call dis(a); // calling addval function addval(a); printf("Array after function call\n"); dis(a); }
๐ŸŒ
Quora
quora.com โ€บ How-can-I-update-an-element-in-an-array-using-C
How to update an element in an array using C - Quora
Answer (1 of 4): Add this line to the beginning of your code [code]#define update(arr,pos,val) do{arr[pos]=val;}while(0) [/code]You will have to use it like this [code]update(name_of_array, position, new_value); [/code]For example: [code]#define update(arr,pos,val) do{arr[pos]=val;}while(0) #i...
Top answer
1 of 8
47

For your specific case where you initially have 0 and 1, the following might be faster. You'll have to bench mark it. You probably can't do much better with plain C though; you may need to dive into assembly if you want to take advantage of "x86 trickery" that may exist.

for(int i = 0; i < size ; i++){
  array[i] *= 123456;
}

EDIT:

Benchmark code:

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

size_t diff(struct timespec *start, struct timespec *end)
{
  return (end->tv_sec - start->tv_sec)*1000000000 + end->tv_nsec - start->tv_nsec;
}

int main(void)
{
  const size_t size = 1000000;
  int array[size];

  for(size_t i=0; i<size; ++i) {
    array[i] = rand() & 1;
  }

  struct timespec start, stop;

  clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start);
  for(size_t i=0; i<size; ++i) {
    array[i] *= 123456;
    //if(array[i]) array[i] = 123456;
  }
  clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &stop);

  printf("size: %zu\t nsec: %09zu\n", size, diff(&start, &stop));
}

my results:

Computer: quad core AMD Phenom @2.5GHz, Linux, GCC 4.7, compiled with

$ gcc arr.c -std=gnu99 -lrt -O3 -march=native
  • if version: ~5-10ms
  • *= version: ~1.3ms
2 of 8
15

For a small array such as your it's no use trying to find another algorithm, and if the values are not in a specific pattern a simple loop is the only way to do it anyway.

However, if you have a very large array (we're talking several million entries), then you can split the work into threads. Each separate thread handles a smaller portion of the whole data set.

๐ŸŒ
Reddit
reddit.com โ€บ r โ€บ learnprogramming โ€บ comments โ€บ 12w5fb โ€บ c_is_it_possible_to_overwrite_a_char_array
[C] is it possible to overwrite a char array?
November 9, 2012 - A subreddit for all questions related to programming in any programming language (Contributions are only allowed in English!).
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 29191884 โ€บ replacing-values-in-2d-arrays-in-c โ€บ 29191943
replacing values in 2d arrays in c - Stack Overflow
The benefit is if you reconsider using a board size of 9x9 then you don't have to change all the 3's to 9's. Rather just change ... @isaac.: If this answer was useful accept or upvote it.
๐ŸŒ
Arduino Forum
forum.arduino.cc โ€บ projects โ€บ programming
How to delete or change an array completely - Programming - Arduino Forum
April 24, 2013 - I'm trying to change an entire array with one command like this: int list[] = {0,1,2,3}; // (later in function) list = {2,4,6,8}; I know this is wrong, but I just don't know how to do it right. The variable I want to cโ€ฆ