Thread: What's wrong with this array initialization?

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    69

    What's wrong with this array initialization?

    Code:
    int main (int argc, char* argv[]) {
        char license_plates[M][N];
        double pay[M];
        int i, j;
        
        
        for (i = 0; (i < M); i++) {
            pay[i] = -1;
            for (j = 1; (j < N); j++) {
                license_plates[i][j] = '-';
            }
            license_plates[i][0] = '\0';
        }
    return 0;
    }
    Whenever I run the gdb debugger on this, it turns out that the license_plates array is only filled with '-' when what I'm clearly trying to have is a '\0' in license_plates[i][0] and a '-' on the rest of each row. Why is this happening?

    I have defined M as 5, N as 11, and have included <stdio.h>.

    The license_plates array is supposed to include strings which are given later on. Is there a better way to initialize an array of strings?
    Last edited by Xpl0ReRChR; 11-23-2011 at 09:09 AM.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I'm guessing that you set a breakpoint at line 12, since that is the last possible place inside the loop:

    Code:
            license_plates[i][0] = '\0';
    Breaks occur before the line they are set on is executed, hence, the '\0' has not been added yet. You could set a break at line 8 instead, and use i-1. Beware that gdb prints 3 digit escaped ascii codes for unprintable characters, so the array will look like "\000----".

    Quote Originally Posted by Xpl0ReRChR View Post
    The license_plates array is supposed to include strings which are given later on. Is there a better way to initialize an array of strings?
    If you mean, a regular null terminated c-string, there is no need to initialize them this way at all. The only reason to do what you are doing is if you plan to write to the string non-sequentially, and want the rest of it left filled with '-'.
    Last edited by MK27; 11-23-2011 at 09:09 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Leave the license plates content, alone for now. First, fill them with good content, then make sure they get the '\0' (NULL char), at the end of them (so leave a bit of room for that).

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    69
    The whole program I'm trying to finish is supposed to have two large arrays, one with license plates (each as a string), and one with payments. It gets input for both in a loop but stops whenever '0' is set as vehicle type. Which means, part of the arrays will be filled, and part of them will be empty.

    Since I have to use the selection sort algorithm later on, I figured I couldn't just initialize the arrays with '-' because they would lack a '\0', so strcmp wouldn't work (or would it?). So I thought, 'empty' slots should have a '\0' at the beggining and '-' after that.

    I did print license_plates[0] using the gdb debugger after the loop for i=0 was done but it still said it contained 11 '-' characters. Why is that?

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Actually you should be seeing '\0', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-'. If you tested the length of your string with strlen() you would see zero returned for length. Normally you would place the '\0' at the end of your string not the beginning. ie: license[i][j-1] = '\0' and you would start your inner loop at zero not one.

    Jim

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    69
    I got it working, thank you guys! Not sure what went wrong before, I had tried placing '\0' at the end, it hadn't worked, but it worked now. I probably made some mistake I was too tired to see.

  7. #7
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Not sure what you did to fix it, but the best approach would be declare
    Code:
    char license_plates[M][N+1]
    to account for the '\0'. Then
    Code:
    for( ... i < M ... ){   
       for(... j < N ... )
          license_plates[i][j] = '-' ;
       license_plates[i][j] = '\0' ;
    }
    Now you have a clean for loop that exits with one char element left for the '\0'.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array Initialization
    By niteshsood in forum C Programming
    Replies: 2
    Last Post: 04-13-2009, 06:22 AM
  2. array initialization & C standard
    By jim mcnamara in forum C Programming
    Replies: 2
    Last Post: 09-12-2008, 03:25 PM
  3. Difference between array initialization
    By 3saul in forum C Programming
    Replies: 2
    Last Post: 02-05-2006, 11:51 PM
  4. Array initialization...
    By eccles in forum C Programming
    Replies: 4
    Last Post: 12-10-2004, 02:18 AM
  5. Something wrong in initialization?
    By Shadow12345 in forum C++ Programming
    Replies: 1
    Last Post: 04-26-2002, 01:42 PM