Thread: how to assign value in two -dimensional array?

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

    how to assign value in two -dimensional array?

    Now , there is a two-dimensional array a[3][4];

    how to assign value by one element? for example
    Code:
         a[1][2] = 'a'; //let the third element of the second line is equal to 'a'

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by zcrself View Post
    Now , there is a two-dimensional array a[3][4];

    how to assign value by one element? for example
    Code:
         a[1][2] = 'a'; //let the third element of the second line is equal to 'a'
    Erm... you answered your own question.

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    168
    directly assign value is not right !

  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
    directly assign value is not right !
    Why?
    zac7 is right, you've answered your question.
    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
    Sep 2006
    Posts
    8,868
    The assignment syntax you showed was correct.

    If things aren't working, then we need to see the rest of the program to see what's gone wrong.

  6. #6
    Registered User
    Join Date
    Aug 2009
    Posts
    168
    Code:
    char snp[785][106];
        //getintomemo(argv[1],snp[785][106]);
    copyintoarray(p,snp[785][106]);
    
    //COPYINTOARRAY: copy what the pointer points into one array
    void copyintoarray(char **p,char arr[785][106])
    {
         int m,n;
         for ( int i = 0; i < m ; i++ && p++ )
         {
             
             for ( int j = 0; j < n ; j++ && *p++ )
             {
                 char a = **p;
                 printf("%c",a);             
             }
             puts("\n");       
         }
    }
    error is as follows:
    invalid conversion from `char' to `char (*)[106]'

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I'm not familiar with that type of for loop. I have serious reservations about i++ && p++, and also j++ && *p++. If you want both i and p to increment, the C syntax would be:

    Code:
    for(i = 0; i < yourVariable; i++, p++)  {
      //rest of your loop code
    }
    Would that be what you wanted?

    The compiler is saying that you're trying to treat a char, like a pointer to char, and it can't do that.

    Post up your whole program - every error occurs within a context of the whole program. I'm not a good enough programmer to tell you what's wrong with code, unless I have seen that type of code, frequently.

    In a whole program however, the problems can't hide nearly as well.

  8. #8
    Registered User
    Join Date
    Aug 2009
    Posts
    168
    Your answer is my want , thanks very much !

    Quote Originally Posted by Adak View Post
    I'm not familiar with that type of for loop. I have serious reservations about i++ && p++, and also j++ && *p++. If you want both i and p to increment, the C syntax would be:

    Code:
    for(i = 0; i < yourVariable; i++, p++)  {
      //rest of your loop code
    }
    Would that be what you wanted?

    The compiler is saying that you're trying to treat a char, like a pointer to char, and it can't do that.

    Post up your whole program - every error occurs within a context of the whole program. I'm not a good enough programmer to tell you what's wrong with code, unless I have seen that type of code, frequently.

    In a whole program however, the problems can't hide nearly as well.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By bennywhere in forum C Programming
    Replies: 16
    Last Post: 10-20-2009, 09:00 PM
  2. Multidimensional Array Addressing
    By BlackOps in forum C Programming
    Replies: 11
    Last Post: 07-21-2009, 09:26 PM
  3. Dynamic Mutli dimensional Array question.
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 02-22-2006, 07:07 PM
  4. two dimensional string array question
    By Hoser83 in forum C Programming
    Replies: 8
    Last Post: 02-07-2006, 08:15 PM
  5. 2d arrays in C
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 04-20-2002, 11:09 AM