Thread: converting char 1d array to 2d array

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    9

    Thumbs up converting char 1d array to 2d array

    In the loop, the elements in the 2d array, d has the right strings. but once it exits the loop, all the elements in d are the last string.. how should i resolve this?

    Code:
    #include<stdio.h>
    #include<string.h>
    
    int main (void)
    {
    char s[]="abcd:efgh:ijkl:\0";
    char *d[100];
    printf("s is %s\n",s);
    char wasi[]="wasi";
    char ahmed[]="ahmed";
    int k = strlen(s); int x=0,y=0; int z=0;
    char c = s[x]; char test[1000]; //memset(test,"0",sizeof(test));
    while(x<k)
    {
    while(c!=':')
    {
    if(c=='\0')
    {break;}
    test[y]=c;
    x++;y++;
    c=s[x];
    }
    test[y]='\0';
    //test contains a string between colons
    printf("test is %s\n",test);
    //printf("z is %d\n",z);
    
    d[z]=test;
    strcat(d[z],"\0");
    printf("d[%d] is %s\n",z,d[z]);
    
    z++;
    x++;c=s[x];
    y=0;
    if(c=='\0')
    {break;}
    }
    d[z]="\0";
    // printing the contents of the string
    int i;
    for( i=0; i<2;i++)
    {
    printf("d[%d] = %s\n",i,d[i]);
    }
    // EVERYTHING INSIDE d IS THE SAME AS THE LAST STRING
    
    
    return 0;
    
    
    }
    Last edited by Salem; 03-19-2011 at 01:05 PM. Reason: Added [code][/code] tags

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Please use code tags when posting code!

    You want the first row to be "abcd", second row to be "efgh", third to be "ijkl", in a char array?

    You seem to have the algorithm basically set up. I'm not clear what the problem is that you're having.

    Using the input in your program, what are getting for output, and what do you want for correct output?

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Even with code tags, the indentation is dog-food.

    Figures, it's also cross-posted Char 1d to 2d array problem C programming? - Yahoo! Answers

    No doubt, the code here was a copy/paste from there to here, and in the process, and semblance of formatting that it might have had has been lost.

    Here, read this before deciding to spam forums again.
    How To Ask Questions The Smart Way
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    9
    k this is the first time im using this..so take it easy..
    The input is abcd:efgh:ijkl

    expected output:
    abcd
    efgh
    ijkl

    output produced:
    ijkl
    ijkl
    ijkl

    why is this happening?

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by wasi.cjr View Post
    k this is the first time im using this..so take it easy..
    The input is abcd:efgh:ijkl

    expected output:
    abcd
    efgh
    ijkl

    output produced:
    ijkl
    ijkl
    ijkl

    why is this happening?
    Because of the logic that is used -- not quite right.

    First, where is the 2D char array? I don't see one. Or do you just want it printed up in rows, like you show for the output, above?

    @Salem - thanks for the code tags, however. That's a lot better than before!

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    9
    isnt char *d[100]; a 2-D array?.. i thought this is a 2d array and i had to convert a 1D array to 2D array where ':' is the token

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by wasi.cjr View Post
    isnt char *d[100]; a 2-D array?.. i thought this is a 2d array and i had to convert a 1D array to 2D array where ':' is the token
    It is. I have a rough time with code not in code tags.


    Let's redo the variables to something more meaningful. Row and col for their respective indices, c for char, and len for the total length of the original string.

    Code:
    #include <stdio.h>
    
    int main(void) {
    
      int len;
      int i = 0;
      int row = 0;
      int col = 0;
      char s[] = {"abcd:efgh:ijkl"};
      char d[3][5];
      char c;
      printf("\n\n");
      len = strlen(s);
      while(i<len) {
        c = s[i];    
        if(c==':') {
          d[row][col]='\0';
          row++;
          col = 0;
        }
        else {
          d[row][col]=c;
          ++col;
        }
        ++i;
      }
      d[row][col]='\0';
      //d[row] contains a string between colons
      for(i=0;i<=row;i++)
        printf("d[%d] is %s\n",i, d[i]);
    
      (void) getchar();
      return 0;
    }
    char *d[5] is not a true 2D char array. It's (reading from right to left: a 5 element array of pointers to char.

    That's very useful sometimes, but then each of those pointers needs to be:

    1) given a valid address
    and
    2) given a number of memory addresses for their row of 5 chars.

    which is not what I would want to do for a program like this.

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    9
    Quote Originally Posted by Adak View Post
    It is. I have a rough time with code not in code tags.


    Let's redo the variables to something more meaningful. Row and col for their respective indices, c for char, and len for the total length of the original string.

    Code:
    #include <stdio.h>
    
    int main(void) {
    
      int len;
      int i = 0;
      int row = 0;
      int col = 0;
      char s[] = {"abcd:efgh:ijkl"};
      char d[3][5];
      char c;
      printf("\n\n");
      len = strlen(s);
      while(i<len) {
        c = s[i];    
        if(c==':') {
          d[row][col]='\0';
          row++;
          col = 0;
        }
        else {
          d[row][col]=c;
          ++col;
        }
        ++i;
      }
      d[row][col]='\0';
      //d[row] contains a string between colons
      for(i=0;i<=row;i++)
        printf("d[%d] is %s\n",i, d[i]);
    
      (void) getchar();
      return 0;
    }
    char *d[5] is not a true 2D char array. It's (reading from right to left: a 5 element array of pointers to char.

    That's very useful sometimes, but then each of those pointers needs to be:

    1) given a valid address
    and
    2) given a number of memory addresses for their row of 5 chars.

    which is not what I would want to do for a program like this.
    Thanks a lot!

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You can of course just pretend it's a 2D array:
    Code:
    char foo[ X ];
    
    foo[ (row * rowlen) + col ]
    This isn't want you are trying to do here, but you could.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 02-23-2010, 06:17 AM
  2. I need help :(
    By ramenen in forum C++ Programming
    Replies: 1
    Last Post: 02-17-2010, 04:31 PM
  3. Quick Question: 2D Array with char **
    By Phoenix_Rebirth in forum C Programming
    Replies: 4
    Last Post: 01-29-2009, 07:33 AM
  4. Treating a 2D array as a 1D array....
    By John_L in forum C Programming
    Replies: 6
    Last Post: 10-18-2007, 02:38 PM
  5. Dictionary into a 2d Char Array... Problems.
    By Muzzaro in forum C Programming
    Replies: 10
    Last Post: 12-02-2006, 12:34 PM

Tags for this Thread