can the some value of array be modified?

This is a discussion on can the some value of array be modified? within the C Programming forums, part of the General Programming Boards category; Can the some value of array be modified? for example: Code: char a[3] = { 'a', 'b', 'c'}; now, modifying ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    168

    can the some value of array be modified?

    Can the some value of array be modified?
    for example:
    Code:
        char a[3] = { 'a', 'b', 'c'};
    now, modifying the second element,
     let    char a[1]   be equal to 'd'
    is it be done ?

  2. #2
    Registered User matrixx333's Avatar
    Join Date
    Mar 2009
    Posts
    67
    Yes,

    You could write something 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;
    }
    I've highlighted the bit of code that actually changes the letter. If you declared your array like:

    Code:
    const char a[3] = { 'a', 'b', 'c'};
    Then all the characters in the array would be constant, and you wouldn't be able to change their values.

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    168
    I understand! thanks
    Does two-dimensional array do also like this ?

  4. #4
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by zcrself View Post
    I understand! thanks
    Does two-dimensional array do also like this ?
    Yes.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  5. #5
    Registered User
    Join Date
    Aug 2009
    Posts
    168
    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]);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multidimensional Array Addressing
    By BlackOps in forum C Programming
    Replies: 11
    Last Post: 07-21-2009, 10:26 PM
  2. Replies: 16
    Last Post: 05-29-2009, 08:25 PM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 08:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21