Thread: Can't seem to print an array, HELP !

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

    Can't seem to print an array, HELP !

    Code:
    #include<ctype.h>
    #include<stdio.h>
    #include<unistd.h>
    
    int main()
    {
    char *array2d[5][5];
    
    int c_1 = 1;
    int c_2 = 2;
    int c_3 = 3;
    
    char ch[50];
    sprintf( ch, "%d %d %d", c_1,c_2,c_3 );
    
    int i,u;
    for(i=0;i<4;i++)
                {
                   for(u=0;u<4;u++)
                        {
                          array2d[i][u] = ch ;
                          
                        }
    
                }
    int y,o;
    for(y=0;y<4;i++)
                {
                   for(o=0;o<4;u++)
                        {
                          printf("%s",array2d[y][o]);
                        }
    
                }
    return 0;
    }

    I need help with this piece of code.
    Basically, I HAVE to have two loops like that; one puts stuff inside an array and another prints it.

    I can't seem to get it to work. Any help is greatly appreciated !!!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Your array is 5 in size. You have 5 elements to work with. You have 0, 1, 2, 3 and 4. Therefore, your loop should be x = 0; x < 5; x++ ... to give you access to all of the elements mentioned earlier. Also, if you are just trying to print out values:
    Code:
    char ch[50];
    sprintf( ch, "%d %d %d", c_1,c_2,c_3 );
    Can just be:
    Code:
    printf( "%d %d %d", c_1,c_2,c_3 );
    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Also... C won't do this --> array2d[i][u] = ch ;

    ch is a string... you cannot assign strings with the = sign.
    Plus your array is malformed... a 2d array[5][5] can only hold 5 strings each 4 characters long. A string is an array in itself.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Ah, I didn't even see that he had a 2D array of pointers. In which case, yes he actually can do this:
    Code:
    array2d[x][y] = ch;
    Because it makes that pointer point to ch. It's not going to be giving him unique individual strings, but it is legal C to make a pointer to char point to a character array (which is what is happening there).


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

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by quzah View Post
    Ah, I didn't even see that he had a 2D array of pointers. In which case, yes he actually can do this:
    Code:
    array2d[x][y] = ch;
    Because it makes that pointer point to ch. It's not going to be giving him unique individual strings, but it is legal C to make a pointer to char point to a character array (which is what is happening there).


    Quzah.
    True, but probably not the result he's looking for.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    8
    Can someone tell me how to make this loop print out values ? yes, I need a loop, not just a line, but a loop, because I will need to do other stuff with it later.


    Code:
    for(y=0;y<4;i++)
                {
                   for(o=0;o<4;u++)
                        {
                          printf("%s",array2d[y][o]);
                        }
    
                }

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Helps if you use standard C idiom's, or at least easy to understand variable names:


    Code:
    for(i=0;i<4;i++)
    {
       for(j=0;j<4;j++)
       {
          printf("%c",array2d[i][j]);
       }
    }
    Good variable names go a LONG way toward creating a good program.

    Remember that a bunch of letters may or may NOT be a string in C. Letters only get elevated to a string, if they have an end of string char, that the program can find: \0. Note that you can't see the end of string char, except in a debugger watch window.
    Last edited by Adak; 03-06-2011 at 11:23 AM.

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    8
    ok, I need need help with this last program:

    Code:
           
    
           FILE *fp; <- this is just a file with numbers;
           << I'm not providing the entire sourse code, since it's irrelevant 
            and it's not related to this code>>
    
           int num=0;int num1=0; int num2=0;  
           int P = 0; int P1 = 0;
           int c_1; int c_2;  int c_3; 
           char ch[50];  
           P = fgetc(fp);  P1 = fgetc(fp); // Just getting first 2 characters from a file, I need this for my program
           fscanf(fp, "%d%d%d",&num,&num1,&num2); // Since numbers in the file are wrriten in "0 1 2" form
           c_1 = num;  
           c_2 = num1; 
           c_3 = num2;
           
    
           char *array2d[c_1][c_2]; // c_1 and c_2 become rows and columns for this array
      
           int row=0; int column=0;
    
            while (fscanf(fp, "%d%d%d",&num,&num1,&num2)==3)
            {
                   sprintf( ch, "%d %d %d", num,num1,num2 );
                   array2d[column][row] = ch ;
                   printf("%s",array2d[column][row]); // THIS PRINTS ALL GOOD !
    
    
                   column+=1;
                   if (column==c_1 && row+1==c_2)
                    {
    
                     break;
    
                    }
    
                    if (column == c_1)
                     {
                        column = 0;
                        row += 1;
                     }
    
            }
            
           int start;
           int finish;
           for (start = 0;start<c_1;start++)
               {
                  for ( finish=0; finish<c_2; finish++)
                  {
                   printf("%s", array2d[start][finish]); // WHEN I PRINT HERE, OUTSIDE OF THE WHILE LOOP, IT PRINTS ALL 0s !!!!
                  }
               }

    I can't seem to print the 2d array outside the while loop, it just prints all 0s !

  9. #9
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    First off, I think you are making life more difficult.
    What are you exactly trying to do?
    Probably needing *[][] is something wrong already.

    Code:
    char buf[100];
    char *p[10] = {0};
    int i = 0;
      while(fgets(buf,sizeof(buf),stdin) ) {     
        p[i++] = buf;                  // omitted bound check
      }
      for(i=0;p[i];i++) {
       printf("%s",p[i]);
      }
    Run above code. What's the output?
    Last edited by Bayint Naung; 03-06-2011 at 12:29 PM.

  10. #10
    Registered User
    Join Date
    Mar 2011
    Posts
    8
    Quote Originally Posted by Bayint Naung View Post
    First off, I think you are making life more difficult.
    What are you exactly trying to do?
    Probably needing *[][] is something wrong already.

    Code:
    char buf[100];
    char *p[10] = {0};
    int i = 0;
      while(fgets(buf,sizeof(buf),stdin) ) {     
        p[i++] = buf;                  // omitted bound check
      }
      for(i=0;p[i];i++) {
       printf("%s",p[i]);
      }
    Run above code. What's the output?

    It's asking my for input, and nothing later.

  11. #11
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Interesting
    p is array of pointer to char.
    When the loop ends, all (say if you entered 3 lines) p[0,1,2] are pointing to same buf.
    now p[0,1,2] contents is last buf content.
    foo
    bar
    egg

    If you entered foo,bar,egg, for loop will print egg,egg,egg. Now you should get the idea.
    you need to allocate memory.

  12. #12
    Registered User
    Join Date
    Mar 2011
    Posts
    8
    ok, it prints:
    linker input file unused because linking not done.

    I just don't understand why, inside the while loop it prints fine, but when I try to iterate the array outside the while loop, it prints the last element for all the array cases, which is 0 0 0 ?

  13. #13
    Registered User
    Join Date
    Mar 2011
    Posts
    8
    I seriously have no idea how to allocate memory in the array, if someone could give me some advice, please

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Demonoid View Post
    I seriously have no idea how to allocate memory in the array, if someone could give me some advice, please
    You don't need to allocate memory. You've already made this more complex than it needs to be, tripping up the job.

    Show an example of the input data, and what you want for output. It's easier by far to work from that, than wade through your code, which we can't run.

    I understand that you need to do the reading and writing in nested for loops, right?

    This is not as hard as you've made it out to be, imo.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Towr of Hanoi move the disc
    By WatchTower in forum C Programming
    Replies: 9
    Last Post: 07-17-2009, 03:48 AM
  2. Print Array in reverse order
    By swgh in forum C++ Programming
    Replies: 6
    Last Post: 11-06-2007, 01:41 PM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. Replies: 10
    Last Post: 07-13-2003, 01:48 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM