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
Answer from Nicu Stiurca on Stack Overflow
🌐
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...
🌐
C For Dummies
c-for-dummies.com › blog
Modifying an Array | C For Dummies Blog
In C, you can easily replace an element in an array: a[5] = 8 (or whatever). But to pluck out an element requires writing a function in C.
Discussions

Changing array inside function in C - Stack Overflow
when you get the pointer in your function its value is 1008(example) so changing that will only mean you now point to another place. that is not what you want. you can change the integers pointed at directly via the * operator, so *array = 99; will change the first element, *(array+1) = 98; ... More on stackoverflow.com
🌐 stackoverflow.com
Change elements in an array - C++ Forum
The last function is supposed to be another bool function to clear either a single element or every element in the array. I was wondering if you guys could help me out with these four functions in any way cause I'm totally lost. ... Similar to the last one. How might you change the value in ... More on cplusplus.com
🌐 cplusplus.com
How do I get the access the last element in an array in C? I'm trying to print 'and' at the end of a sentence in a for loop and think I need to write an if statement but I don't know how to access the last element / write the code. Please and explain, absolute beginner here.
The final element of an array is at index [length - 1]. I'd suggest moving printing the final country outside the loop, which makes adding the "and" trivial and removes the need to check a condition that will only hole once inside the loop. More on reddit.com
🌐 r/C_Programming
73
10
May 4, 2021
I'm struggling with updating an array of objects in vuex, in particular I'm not sure how to bind a text field in a for loop to the item I need to edit. Code in comments...

I think you want something like

<v-text-field name="brandEdit" :value="brand.name" @input=updateBrand"(brand, $event)"></v-text-field>

$event should be your updated value, and you've passed in all your brand data.

Does that help?

More on reddit.com
🌐 r/vuejs
4
4
July 18, 2019
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.

Top answer
1 of 10
31

In c you can't pass a variable by reference, the array variable that you assign inside the function contains initially the same address as the passed pointer, but it's a copy of it so modifying it will not alter the passed pointer.

You need to pass the address of the pointer in order to be able to alter it, like this

// Change the pointer of the array
void change(int **array, int length)
{
    *array = malloc(length * sizeof(int));
    if (*array == NULL)
        return;
    for (int i = 0 ; i < length ; i++)
        (*array)[i] = 1;
}

Then in main() you cannot assign to an array, doing so through this kind of function is surely undefined behavior. The array defined in main() is allocated on the stack and you cannot assign anything to an array since they are non-writeable lvalues so you cannot make it point to a heap memory location obtained with malloc(), you need to pass a pointer like this

int *array;
change(&array, length);
free(array);

If you want the function to replace the previous array, it will have to free() the malloc()ed data (note that passing NULL to free() is well defined), so

// Change the pointer of the array
void change(int **array, int length)
{
    free(*array);

    *array = malloc(length * sizeof(int));
    if (*array == NULL)
        return;
    for (int i = 0 ; i < length ; i++)
        (*array)[i] = 1;
}

then in main()

int *array;
array = NULL;
change(&array, length);
change(&array, length);
change(&array, length);
change(&array, length);
free(array);

will do what you apparently want.

2 of 10
3

Ok, i will make the answer short.

  1. Arrays are always passed by reference in C

change(array, length); In this line, it means reference to the first element of the array variable is passed to the function.

Now the function needs a pointer to catch the reference, it can be a pointer or it can be an array. Note that the pointer or the array is local to the function.

  1. You received it in a pointer named array. Which is definitely a pointer but it is not the same as array in main function. It is local to the change function.

  2. You declared a new array dynamically, and assigned a pointer named new with it. And all the changes you did were to new pointer.

Everything is ok till now.

  1. Now you assigned the new pointer to the array pointer which is local to the change function.

This is the real issue. As even though the array is in heap section, and it will remain in the memory, but the *array and *new are local pointer variables.

Also array pointer in change function is different from array pointer in main function.

  1. The solution to this problem is Manipulate the data of array pointer directly.

    void change(int *array,int length) { int i; for(i = 0 ; i < length ; i++) array[i] = 1; }

In this way you are directly overwriting values of array in main function. Everything else is correct.

Summerization: array pointer in change function is local to change function. array in main function is local to main function. Making change function's local array pointer point to array in heap section won't change data in actual array. You changed pointer's position, Not the array's data.

🌐
GeeksforGeeks
geeksforgeeks.org › c language › changing-array-inside-function-in-c
Changing Array Inside Function in C - GeeksforGeeks
July 23, 2025 - // 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); } Output ·
🌐
YouTube
youtube.com › watch
How To Replace Elements Of Array in C / C++ - YouTube
c program to update an element in an arrayc++ program to replace an element in an arrayhow to change values in an array c++modify array in function carray up...
Published   December 21, 2017
🌐
w3resource
w3resource.com › c-programming-exercises › basic-declarations-and-expressions › c-programming-basic-exercises-51.php
C Program: Print the elements of the modified array - w3resource
Write a C program to read an array of length 6, change the first element by the last, the second element by the fifth and the third element by the fourth. Print the elements of the modified array. ... #include <stdio.h> #define AL 5 int main() { // Define the size of the array int array_n[AL], i, temp; // Prompt user to input 5 elements of the array printf("Input the 5 members of the array:\n"); for(i = 0; i < AL; i++) { scanf("%d", &array_n[i]); } // Swap the first half of the array with the second half for(i = 0; i < AL; i++) { if(i < (AL/2)) { temp = array_n[i]; array_n[i] = array_n[AL-(i+1)]; array_n[AL-(i+1)] = temp; } } // Print the modified array for(i = 0; i < AL; i++) { printf("array_n[%d] = %d\n", i, array_n[i]); } return 0; }
Find elsewhere
🌐
Codeforhunger
codeforhunger.com › 2021 › 05 › C program to replace array element.html
codeforhunger: C program to replace an element in an array with its position
May 15, 2021 - In these example we will learn how to replace or insert new element in an array. Ex: Write a C program to replace an element in an array with its position, How to write a C program to replace an element from array, C program to insert and replace element from array.
🌐
Quora
quora.com › How-can-I-change-the-values-of-an-array-with-a-void-function-in-c
How to change the values of an array with a void function in c - Quora
Answer (1 of 6): There are two ways. 1. Suppose if the array is declared as global. You could simply change the values of array by assignment. 2. If the array your are talking about is not declared as global then pass the address of the array. To do that use & operator. Eg foo(&myarray). Once yo...
🌐
Cprogramming
cboard.cprogramming.com › c-programming › 32620-change-value-array.html
Change Value in an array
January 18, 2003 - I want to change the value in an index of an array I have partially posted the code where I am having problems with: [code] array[6]={2.02,2.05,3.20,3.28,5.0,5.0}; float score; if (score!=array[selection1]) { score=array[selection1]; } [\code] What I am trying to do is to change the appropriate value in the array with the value in score. e.g. if score was entered as 3.0 and was replacing 2.05, how do you get the program to change the 2.05 value in the array? Thank for any help ... Couple of points:- Code tags end with &#91;/code], not [\code] You should declare array as:- float array[6] = {2.02,2.05,3.20,3.28,5.0,5.0}; What type is selection1? Remember, indexes in C range from 0 to n-1, where n is 6 here. ... View Forum Posts .... ... >score=array[selection1]; This assigns the value of the element of array with index selection1 to score.
🌐
Tutorjoes
tutorjoes.in › c_programming_tutorial › update_element_using_one_dimensional_array
Update array element using One Dimensional Array in C
The program then uses a for loop to iterate through the array and checks if the current index of the loop is equal to the position entered by the user. If it is, the program replaces the value at that index with the new value entered by the user. Finally, the program prints the updated array with the updated value at the specified position. #include<stdio.h> int main() { int i,t,a[10],n,m,s,j=0,b[10]; printf("\nEnter the Limit:"); scanf("%d",&n); printf("\nEnter the Values:"); for(i=0;i<n;i++) { scanf("%d",&a[i]); } printf("\nGiven values are:"); for(i=0;i<n;i++) { printf("a[%d]=%d",i,a[i]); } printf("\nEnter the position to be update:"); scanf("%d",&t); printf("\nEnter the value to be update:"); scanf("%d",&s); for(i=0;i<n;i++) { if(i==t) { a[i]=s; } } printf("\nUpdated value is:"); for(i=0;i<n;i++) { printf("\na[%d]=%d",i,a[i]); } return 0; } To download raw file Click Here
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › replace-every-element-of-the-array-by-its-previous-element
Replace every element of the array by its previous element - GeeksforGeeks
September 12, 2022 - // C program to replace every element ... every element // of the array with the element that appears before it void updateArray(int arr[], int n) { // Update array for (int i = n - 1; i > 0; i--) arr[i] = arr[i - 1]; // Change ...
🌐
Zhu45
zhu45.org › posts › 2017 › Jan › 08 › modify-array-inside-function-in-c
Modify array inside function in C - Zeyuan Hu's Page
Before:1 2 3 before change, test address: 0x7fffffffe050 array address inside function: 0x7fffffffe050 After:5 5 5 after change, test address: 0x7fffffffe050 ... If we take a look at what value hold the address, we can see that it's 1, which is the first element of our int test[3] array.
🌐
SunShine Softwares
sunshinesoftwares.com › home › questions › write a c program to replace an element from an array at specified position.
Write a C Program to replace an element from an array at specified position. - SunShine Softwares
July 15, 2022 - ShriRam Changed status to publish July 15, 2022 · Active · Newest · Oldest · ShriRam Posted July 15, 2022 0 Comments · #include<stdio.h> #include <conio.h> void main() { int arr[100],pos,newvalue; int i,n; printf("Enter size of the array : "); scanf("%d", &n); printf("\nEnter array Elements now: "); for(i=0;i<n;i++) scanf("%d", &arr[i]); printf("\nEnter Position of Element : "); scanf("%d", &pos); printf("\nEnter New Value: "); scanf("%d", &newvalue); arr[pos-1]=newvalue; printf("\nModified Elements Are: "); for(i=0;i<n;i++) printf("\n%d", arr[i]); getch(); } ShriRam Changed status to publish July 15, 2022 ·
🌐
CodeCrucks
codecrucks.com › home › programs › c program to replace array elements by given value
C Program to Replace Array Elements by Given Value - CodeCrucks
March 23, 2023 - // C Program to Replace Array Elements by Given Value #include <stdio.h> int main() { int A[] = {1, 6, 5, 8, 6}, n, k, i, j, flag=0; n = sizeof(A) / sizeof(A[0]); printf("Array A :--> "); for(i=0; i<n; i++) printf("%d ", A[i]); printf("\nEnter the Element to replace :--> "); scanf("%d", &k); for(i = 0;i < n; i++) { if(A[i] == k) { int r; flag = 1; printf("Enter Element Replaced with %d:--> ",i); scanf("%d", &r); A[i] = r; } } if(flag == 0) { printf("\nThe element not found in the array"); } else { printf("\nArray After Replacement :--> "); for(j = 0; j < n; j++) { printf("%d ",A[j]); } } return 0; }
🌐
Cplusplus
cplusplus.com › forum › beginner › 180878
Change elements in an array - C++ Forum
This: sum += array [num+1]; should be sum += array [num]; Also you should wait until the end of the loop before computing avg, just for efficiency. ... I was thinking of using a for loop to search through the array and then using an if statement to see if the user inputted element matches an ...
🌐
Unstop
unstop.com › home › blog › arrays in c | declare, manipulate & more (+code examples)
Arrays In C | Declare, Manipulate & More (+Code Examples)
March 18, 2024 - We then employ a for loop to print ... To change an element in the array, we use the array name followed by the index within square brackets to access the specific element we want to modify....
🌐
Quora
quora.com › How-do-you-update-an-element-in-an-array-in-C
How to update an element in an array in C++ - Quora
Arrays in C are never empty. They are always full. What they are full of is bytes, and unless you put something there, whatever byte values there were in memory before the memory was allocated is what you get. Garbage. Pick a value that does not enter into your processing model for an “empty” value, or designate a global “empty object” and copy it into every element ...
🌐
Quora
quora.com › How-do-I-change-the-position-of-an-element-in-an-array-in-C
How to change the position of an element in an array in C++ - Quora
Answer (1 of 3): That depends of course on “the rules” of how you want to keep the array organized. * You can swap values with any other position, that is the simplest way. But they your previous ordering is probably broken. * You can copy the value from its original position, then shift ...