array comparison

This is a discussion on array comparison within the C Programming forums, part of the General Programming Boards category; Hi all. Here's my problem. I have 2 character arrays that are being used to store strings. I want to ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    100

    array comparison

    Hi all. Here's my problem. I have 2 character arrays that are being used to store strings. I want to compare array one with array 2 to see if they both have the same contents in them but it isnt working unfortunately. I this it has something to do with the trailing \n becasue array one gets its string by the user entereing a string via the scanf function. While array 2 uses the fget command red from a text file (it reads correctly from the text file). Here is what the code looks like (note, im writing this code now, its not the exact code im using as i dont have it with me, its at my uni).
    Code:
    #include <stdio.h>
    
    FILE * fptr;
    
    main()
    {
      fptr = fopen("linktofile.txt","r");
      char test1[15] = "";
      char test2[15] = "";
      scanf("%s", test1);
      fget(test2,15,fptr);
      if (test1==test2){
        printf("works");
      }
    }
    sorry if my coding is bad, im very new to C, only started learning it yesterday. So can anyone see where im going wrong? My guess is with the if statement conditions.

    Also is there a way to take a section of an array? For example, my code has 2 arrays both with a 15 index. Is there away for me to tell C that i only want to look at index 0 to 4. So i could do something like this

    Code:
    if (test1[0..4] ==test2[0..4])
    Thanks a lot

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,260
    Code:
    if(!strncmp(test1,test2,4))
      printf("The first 4 indexes are equal\n");

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >if (test1==test2){
    This doesn't work the way you would expect. The short story is that when you use an array, it is converted to a pointer to the first element. Therefore, the above code is comparing two pointers to see if the point to the same address. They do not, so the test will always fail.

    Anytime you want to work with the contents of a string, you generally need to use a standard library function. In this case, either strcmp or strncmp as bithub1 suggested will work.

    The nice thing about C arrays being converted to pointers is that you can easily compare subsets of the array that do not start at index 0:
    Code:
    /* Test the first four characters starting at index 5 */
    if ( strncmp ( s1 + 5, s2 + 5, 4 ) == 0 )
    By easily I mean avoiding subscripting and the address-of operator. Here is the equivalent alternative:
    Code:
    /* Test the first four characters starting at index 5 */
    if ( strncmp ( &s1[5], &s2[5], 4 ) == 0 )
    Which you choose is up to you, but it's nice to have the option, no?
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-25-2008, 01:50 AM
  2. array of pointers/pointer arithmetic
    By tlpog in forum C Programming
    Replies: 18
    Last Post: 11-09-2008, 07:14 PM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 05:15 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM

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