Like Tree2Likes
  • 1 Post By CommonTater
  • 1 Post By laserlight

Printing an array of strings until array == NULL

This is a discussion on Printing an array of strings until array == NULL within the C Programming forums, part of the General Programming Boards category; Hi, I have a weird problem that I have no idea how to solve. Here's the code and the output: ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Location
    Southampton, United Kingdom
    Posts
    7

    Printing an array of strings until array == NULL

    Hi,

    I have a weird problem that I have no idea how to solve.

    Here's the code and the output: C code - 19 lines - codepad

    The jist of my problem is that:

    Code:
    while(array[i][SL] > 0){
    works OK, but displays one less value of array[i][SL] than it should. So what I've done is edit the code like so:

    Code:
    while(array[i-1][SL] > 0){
    But I am aware that this is simply masking the real problem. Please help!

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Post your code here... don't be making us chase it.
    laserlight likes this.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Location
    Southampton, United Kingdom
    Posts
    7
    Code:
    #include <stdio.h>
    
    
    #define AL 100
    #define SL 32
    
    
    int main(){
        char array[AL][SL] = { "at", "be", "funzey", "zebra" };
        int i,used = 0;
        //i want to find out how much of the array is in use.
        for(i=0;i<AL;i++){
            if(array[i][SL]!=NULL){
                printf("Slot #%i = %s\n",i,array[i]);
                used++;
            }
        }
        printf("Slots used: %i\n",used);
        printf("Slots remaining: %i\n",AL-used);
        return 0;
    }
    OUTPUT:
    Slot #0 = at
    Slot #1 = be
    Slot #2 = funzey
    Slots used: 3
    Slots remaining: 97
    Codepad is pretty nice, though. You can edit my code, compile it and run it without leaving the page.
    Last edited by mikemhz; 11-04-2011 at 11:56 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,633
    Uh, it might not be intentional, but it looks like your posted code is completely formatted wrongly. If you are using some external bbcode/HTML based syntax highlighting tool, ditch it and just post your "raw" code as syntax highlighting will be done for you.
    mikemhz likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2011
    Location
    Southampton, United Kingdom
    Posts
    7
    fixed

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by mikemhz View Post
    Code:
    #include <stdio.h>
    
    
    #define AL 100
    #define SL 32
    
    
    int main(){
        char array[AL][SL] = { "at", "be", "funzey", "zebra" };
        int i,used = 0;
        //i want to find out how much of the array is in use.
        for(i=0;i<AL;i++){
            if(array[i][SL]!=NULL){
                printf("Slot #%i = %s\n",i,array[i]);
                used++;
            }
        }
        printf("Slots used: %i\n",used);
        printf("Slots remaining: %i\n",AL-used);
        return 0;
    }
    Codepad is pretty nice, though. You can edit my code, compile it and run it without leaving the page.
    Our job is to help you fix your own code... we are not here to fix it for you.

  7. #7
    Registered User
    Join Date
    Oct 2011
    Location
    Southampton, United Kingdom
    Posts
    7
    Ok mate, just saying. Who knows, you might need to play around with it to come up with a solution yourself... I thought I was doing you a favour, not asking you to do my homework for me...

    Thank you for being here none-the-less. You are a valued human being. I will hopefully owe you 20 minutes of not asking at another forum for helping me.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,633
    The thing is, array[i][SL] is not a pointer. Furthermore, it is actually out of bounds of each individual array, but because they are all part one a 2D array, it is not out of bounds of the 2D array except for the last array.

    Rather, compare array[i][0] to '\0', since you are searching for the first empty string.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Oct 2011
    Location
    Southampton, United Kingdom
    Posts
    7
    Good shout. Thanks

    Would you mind clarifying what an array of strings is, other than a pointer?

    I understand that int array[5]; is used like a pointer...

    e.g.

    Code:
    func(int * a){
         stuff();
    }
    
    int main(){
         int array[5];
         func(array); //rather than func(&array);
    }
    Last edited by mikemhz; 11-04-2011 at 12:20 PM.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,633
    Quote Originally Posted by mikemhz
    Would you mind clarifying what an array of strings is, other than a pointer?
    An array of strings is not a pointer

    Quote Originally Posted by mikemhz
    I understand that int array[5]; is used like a pointer...
    When an array is passed as an argument, it is converted to a pointer to its first element.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,301
    Look at these two lines.
    Code:
            if(array[i][SL]!=NULL){
                printf("Slot #%i = %s\n",i,array[i]);
    The second line happily passes a pointer to the string you want to output and all is well. So why is the first line doing something different with the array?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to set an array slot to NULL
    By kotoko in forum C Programming
    Replies: 20
    Last Post: 01-06-2009, 02:06 PM
  2. Assign null to a character array.
    By mahedee in forum C Programming
    Replies: 1
    Last Post: 08-06-2008, 07:16 AM
  3. Replies: 2
    Last Post: 04-27-2008, 03:39 AM
  4. Character Array: If Null
    By Drainy in forum C Programming
    Replies: 3
    Last Post: 04-14-2005, 03:06 PM
  5. printing an array of strings
    By linucksrox in forum C Programming
    Replies: 3
    Last Post: 05-11-2004, 03:31 PM

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