Can the some value of array be modified?
for example:
is it be done ?Code:char a[3] = { 'a', 'b', 'c'};
now, modifying the second element,
let char a[1] be equal to 'd'
Printable View
Can the some value of array be modified?
for example:
is it be done ?Code:char a[3] = { 'a', 'b', 'c'};
now, modifying the second element,
let char a[1] be equal to 'd'
Yes,
You could write something like
I've highlighted the bit of code that actually changes the letter. If you declared your array like:Code:#include <stdio.h>
int main(void)
{
char a[3] = { 'a', 'b', 'c'};
int i;
for (i=0; i<3; i++)
printf("a[%d]: %c\n", i, a[i]);
// change a[1] to the letter 'd'
a[1] = 'd';
printf("\n");
for (i=0; i<3; i++)
printf("a[%d]: %c\n", i, a[i]);
return 0;
}
Then all the characters in the array would be constant, and you wouldn't be able to change their values.Code:const char a[3] = { 'a', 'b', 'c'};
I understand! thanks
Does two-dimensional array do also like this ?
I do a try , correct
thanks !
Code:char b[3][3] = {{'1','2','3'},{'4','5','6'},{'7','8','9'}};
b[0][0] = 'c';
for(int m = 0; m < 3; m++)
for(int n = 0; n < 3; n++)
printf("%c ",b[m][n]);