Thread: comapring specific locations in an array

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    31

    comapring specific locations in an array

    Hi

    i have an array and wish to compare certain parts of the array to a string. For example i have the string "AB CD EF" and i want to test my string to see if it is of AB or CD or EF


    this is what i have done but does not seem to work

    Code:
    char temp[10];
    char to_b_tested[10];
    
    strncpy(temp, "AB CD EF", 8);
    if ((strncmp(temp[0], to_b_tested, 2) == 0) || 
        (strncmp(temp[3], to_b_tested, 2) == 0) || 
        (strncmp(temp[6], to_b_tested, 2) ==0))
    {
        ;//then do something
    }
    any hints?

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    31
    assuming that there is something already in to_b_tested

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    if( strncmp( temp + offset, to_b_tested, 2 ) == 0 )
    Try that method. Instead of indexing the array, which gives you the character, not the address of it, use the above. Or apply the address-of operator to what you have. Remember, the function wants a pointer, not a character.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    31
    thank you. also is %x to print in hex ?

  5. #5
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by mbooka
    thank you. also is %x to print in hex ?
    You would know the answer to that if you (a) tried it or (b) looked up printf().

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    It's like he thinks his computer will explode if he tries something without knowing its outcome.
    Sent from my iPadŽ

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    31
    to be honest i already tried it before posting it. There are so many ways to do one thing and i was just seeing if there are better ways to do one thing.

    Is there a site that has good c programming references? Either i am bad at using the search engine or there is not many good sites out there. (Personally i think i sux at using the search engine =p)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  2. Code: An auto expanding array (or how to use gets() safely).
    By anonytmouse in forum Windows Programming
    Replies: 0
    Last Post: 08-10-2004, 12:13 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Array help
    By deedlit in forum C Programming
    Replies: 4
    Last Post: 11-05-2003, 10:55 AM
  5. mode of an array
    By Need Help in forum C Programming
    Replies: 15
    Last Post: 09-17-2001, 08:03 AM