Array problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Parul Bagadia
    New Member
    • Mar 2008
    • 188

    Array problem

    Code:
     
    while(ch!=' ')
            {
          [B]frgt[fr]=ch;
    [/B]      ch=getc(in);
          fr++;
            }
            fr=0;
            [B]if(frgt=="DB"|| frgt=="db")[/B]     //making entry in symbol type field of symbol table.
          {
            strcpy(syb_type,"Byte");
            loc_cntr=loc_cntr+1;      //Incrementing location counter values according to the data types encountered.
            syb_add[sba]=loc_cntr;
            sba++;
            syb_add[sba]=' ';
            sba++;
          }
            else if(frgt=="DW" || frgt=="dw")
          {
             strcpy(syb_type,"Word");
             loc_cntr=loc_cntr+2;
             syb_add[sba]=loc_cntr;
             sba++;
             syb_add[sba]=' ';
             sba++;
          }
            else if(frgt=="DD" || frgt=="dd")
          {
            strcpy(syb_type,"Double Word");
            loc_cntr=loc_cntr+4;
            syb_add[sba]=loc_cntr;
            sba++;
            syb_add[sba]=' ';
            sba++;
          }
             while(ch!='\n')
               ch=getc(in);
             ch=getc(in);
    while debugging this program frgt; which is acharachter array has the value "DB" ;but it doen't go in if loop having condition if(frgt=="DB" || frgt=="db").
    What can be the reason?
  • Studlyami
    Recognized Expert Contributor
    • Sep 2007
    • 464

    #2
    You can't compare strings with the == operator. If you want to check a character you can use the == operator, but if you want to check for a string use the strcmp() function.

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      Let's assume that you initialized fr (to zero) and ch (to getc()) before entering the while-loop. Let's assume that frgt is declared as a char array.

      Studlyami is right, you need to call a function to compare two strings. However, you do not yet have a string in the frgt array. You need to write the null character to the end of the character stream within the array to make it a string.

      By the way, what protects you from writing past the end of the frgt array? To make your code safe you need to exit the loop just before the array fills up (remember, you need to reserve room for the null character).

      Don't confuse the null character with the null pointer. They are two completely different things that happen to have similar names.

      Comment

      • Parul Bagadia
        New Member
        • Mar 2008
        • 188

        #4
        Originally posted by donbock
        You need to write the null character to the end of the character stream within the array to make it a string.

        Don't confuse the null character with the null pointer. They are two completely different things that happen to have similar names.
        I checked the value of array, while i was debugging and found that at the end of the array there was a null charachter in the array it was \x0..
        so i could have equated the array ending with null charachter with string, array ending with null charachter becomes a string , right?

        Comment

        • donbock
          Recognized Expert Top Contributor
          • Mar 2008
          • 2427

          #5
          It is better practice to explicitly write the values you need into the array rather than to trust that the right value will somehow happen to be there on its own.

          It is true that the C Standard requires static variables without any explicit initializer to be set to all zeroes before your program begins to execute. However, I suggest that relying on that behavior creates a situation where it is hard for a reviewer or maintainer to understand how the program works. This objection is removed if you carefully and fully document this assumption in a comment.

          Even with a good comment, this assumption breaks down if your program loops around to use the variable a second time.

          Comment

          Working...