Thread: Char arrays

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    5

    Question Char arrays

    I really need help with this one. Very confused with arrays... I always get FALSE in this program. How to make them match?
    Source:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <windows.h>
     
    
    int main()
    {
        char array1[10],array2[10],array3[10];
        array1[10]='h','e';
        
        array2[10]='l','l','o';
        array3[10]='h','e','l','l','o';
        if(((array1,array2)==array3)==TRUE) 
        {
                                          printf("THEY MATCH"); 
                                          system("PAUSE");
                                          }
                                          if(((array1,array2)==array3)==FALSE)
                                          {
                                                                            printf("THEY DONT MATCH");
                                                                            system("PAUSE");
                                                                            }
                                                                            }

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    23

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Code:
    char array[10]
    will create an array of 10 characters.
    The first character is on 0 index, the 2nd on the 1 index and so on... A for loop is what we need to go over all the elements.
    Code:
    char array[10 + 1];
    int i;
    for(i = 0 ; i < 10 ; i++)
       array[i] = 'h';
    If you print the array with %s in the printf(), you will see a word of 10 letters. Notice, that I allocated an array of 11 characters. 10 characters for the word, plus one for the null terminator for the strings!

    If you have
    Code:
    char array[10];
    
    array[10] = "h" <---error
    you are accessing memory that is not yours, it's not allocated, since indexing starts from zero and finishes in N - 1 (here N is 10).

    Moreoer, you need to check string.h library. For example there, you will find strcpy and strcmp functions, for copying strings and comparing strigns, respactively.

    Strings in C, is a part that confuses many people, so don't give up.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    What are you even trying to do with this code? I'm quite sure (array1,array2) doesn't do what you think, and you can't compare arrays with ==, you need to compare them element-by-element (e.g. for loop).

    windows.h and system("PAUSE") are OS-specific, it's best to avoid them where possible. After I fixed that, here's what I got when I compiled:
    Code:
    $ make arr
    gcc -Wall -ggdb3 -pedantic -std=c99 -O0 -o arr arr.c -lm -lpthread -lrt
    arr.c: In function ‘main’:
    arr.c:11:49: warning: left-hand operand of comma expression has no effect [-Wunused-value]
    arr.c:14:16: warning: left-hand operand of comma expression has no effect [-Wunused-value]
    arr.c:19:16: warning: left-hand operand of comma expression has no effect [-Wunused-value]
    You should see similar warnings. If not, turn the warning level up on your compiler. The "comma expression" is (array1,array2).


    Even if it the comma operator did concatenate arrays, array1 and array2 are each 10 chars, so your comparison would fail since there would be 8 garbage values between the 'h', 'e' in array1 and the 'l', 'l', 'o' in array2. Neither are array1, array2 or array3 strings since they are missing the null terminator that C requires on all strings, so functions like strcat and strcmp would also not work.

    As for (array1,array2), that expression yields array2, so
    Code:
    (array1,array2)==array3
    // is reduced to
    array2==array3
    Also, the language will let you compare, e.g., array1 == array2, but it does not do what you think. When you use an array name without any [ ] brackets, it gives you a pointer to the first element. Thus, array1 == array2 checks if the first element of array1 is in the same memory location as the first element of array2. Since they're different arrays, and occupy different spots in memory, it will always be false.

    I suggest getting a couple good books, and finding some online tutorials, and reading the array sections carefully. Work through all the practice problems you can find. Even copy the example programs from the books, and try changing small pieces to see what happens.

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by root View Post
    I think that you miss what can occur by using only strcat.

    Here is an example
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
            char buf[30];
            strcat(buf, "hello");
            strcat(buf, " ");
            strcat(buf, "world!");
            printf("%s\n", buf);
            return 0;
    }
    This will output (if it doesn't crash), in my pc
    Code:
    ������V���m�%�V��Rp�hello world!
    In other words, undefined behavior!

    However, if you use strcpy first and then strcat, you are ok
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
            char buf[30];
            strcpy(buf, "hello");
            strcat(buf, " ");
            strcat(buf, "world!");
            printf("%s\n", buf);
            return 0;
    }
    which outputs
    Code:
    hello world!
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    23
    Quote Originally Posted by std10093 View Post
    cut
    My idea for doing the test was:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    int main(void){
    
    
       char array1[10]="he";
    
       char array2[10]="llo";
    
        char array3[10]="hello";
    
    
        if(!strcmp(strcat(array1,array2),array3)){
            printf("THEY MATCH\n");
        }
        else{
            printf("THEY DONT MATCH\n");
        }
    
        return 0;   
    }
    
    


    But to be pedantic, in this way OP changes the array1 content, so he should create a new array and use strcpy as you suggest.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 05:31 AM
  2. hex in char arrays :S
    By Elyubarov in forum C++ Programming
    Replies: 33
    Last Post: 12-28-2004, 01:19 PM
  3. Help Please. Char Arrays
    By n00by in forum C Programming
    Replies: 5
    Last Post: 05-27-2004, 11:10 PM
  4. char arrays
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 01-29-2002, 12:04 PM
  5. char arrays
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 12-17-2001, 10:36 AM