Char array

This is a discussion on Char array within the C Programming forums, part of the General Programming Boards category; Code: int main(void) { . . . char row[15]; } int function(char *row) { char numbers[16] = { "0", "1", ...

  1. #1
    Deleted Account
    Join Date
    Mar 2005
    Posts
    57

    Char array

    Code:
    int main(void)
    {
       .
       .
       .
       char row[15];
    }
    
    int function(char *row)
    {
        char numbers[16] = { "0", "1", ........"15" };
       
    
       for(i=0; i<size; i++)
       { 
          if(....= ...)
               [Code help needed here! ]
      }
    }
    ok..i pasted a very small portion of my assignment that i'm doing...anywayz....the problem i'm have is trying to assign the "row" to a value from "numbers"....how wud i approach doing this, i tried several ways, they work, but from the numbers 1-9, any higher, it'll juz take the first number, i.e 15, will become 1.

    this was one of them -> *row = *(numbers[i]); but yea, that's buggy...

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    Code:
    char numbers[16] = { "0", "1", ........"15" };
    " " is used to denote strings, a string is an array of chars.

    and if u want it to read numbers try,
    char numbers[16]={1,2,3,4,.....15}
    Last edited by qqqqxxxx; 03-06-2006 at 12:47 AM.

  3. #3
    Deleted Account
    Join Date
    Mar 2005
    Posts
    57
    but digits from 10 onwards are two, so i have to use " "....

    i'm pretty sure, i can do " "...

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    no u cant, unless u use a pointer.
    did u try compiling char numbers[15] ={"1","2"....

    u will get an error.

    anyways ,just use char numbers[15]={1,2,3,4...15}

    this will take in both numbers.
    Code:
     

  5. #5
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    Code:
    char numbers[16];
    That declares an array of 16 chars. A char can only hold one character. "15" is a 3 char array consisting of the chars '1', '5', and '\0'. It cannot fit in a char.

  6. #6
    Deleted Account
    Join Date
    Mar 2005
    Posts
    57
    Quote Originally Posted by qqqqxxxx
    no u cant, unless u use a pointer.
    did u try compiling char numbers[15] ={"1","2"....

    u will get an error.

    anyways ,just use char numbers[15]={1,2,3,4...15}

    this will take in both numbers.
    Code:
     

    sorri, a typo! char* numbers[....]...hehe...anywayz how wud i fix it guys, thanks alot...

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    Code:
    strcpy(row_p+i,numbers+i);

    here row should be a double matrix
    char row[15][3];
    char (*row_p)[3] ;
    row_p=row;
    Last edited by qqqqxxxx; 03-06-2006 at 01:09 AM.

  8. #8
    Deleted Account
    Join Date
    Mar 2005
    Posts
    57
    hmmm...well i only need to store only one of from 1-15...to row..so why do i need such a big array, actually cud u explain a bit about the double matrix....so it wudn't work with my way?

  9. #9
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    u dont need that actually,only because u r insisting on using char *numbers[15].


    if u only need numbers from 1-15 ,
    just use

    char numbers[15]={0,1,2,3,......15}


    and using a number without any quotes forces it to be stored in decimal (%d) format.

    and a char is 8 bit so u can store a number using decimal format as long as it is < =127

    and then u can have char row[15];

    and u can do an assignment like this row[12]=numbers[15]

    Code:
     
    Last edited by qqqqxxxx; 03-06-2006 at 01:59 AM.

  10. #10
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    two dimensional arrays are declared like this m[2][3]

    u can think of it as having 2 rows and 3 columns,so it would look somewhat like this:




    xxx c0 | c1 | c2
    ________________________________
    row0 1 | 2 | 3

    row1 4 | 5 | 6



    that would mean that m[0][0]==1
    m[1][0]==4
    m[1][1]==5 ...... and so on.


    a pointer to a double matrix char m[2][3] is declared like this
    char (*m_p)[3];

    now,
    m_p=m; //here m_p points to first row of m[2][3]


    m_p+1 will point to second row of m[2][3];

    so ( *(m _p+1))[0]==4 //check the parenthesis,u have to get them right everytime for
    // this to be able to work.

    Code:
     
    Last edited by qqqqxxxx; 03-06-2006 at 02:17 AM.

  11. #11
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    try this
    Code:
    char row[14];
    row[12]= 15;
    printf("%d",row[12]);

    here is the output:
    15

  12. #12
    Deleted Account
    Join Date
    Mar 2005
    Posts
    57
    lol..my bad, i was assiging it to the wrong array, lol..thanks alot for the help, really appreciate the quick replies! ...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-17-2008, 11:36 AM
  2. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 09:22 AM
  3. code help required
    By Yobbo in forum C Programming
    Replies: 9
    Last Post: 09-02-2005, 11:15 PM
  4. Creating 2D arrays on heap
    By sundeeptuteja in forum C++ Programming
    Replies: 6
    Last Post: 08-16-2002, 11:44 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

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