Thread: help with pointers and ints

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    67

    help with pointers and ints

    I am writing code that searches a string for a specified character and returns its position in the array. Here's my code so far:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int check_for_label (char str[])
    {
        char * pch;
        int i;
        for (pch = strchr (str, ':'); pch != NULL; pch = strchr (pch + 1,':'))
        {
            i = pch - str + 1;
        }
        return i;
    }
    int main ()
    {
        int h;
        char label[5];
        char str[] = "for :add $2, $3, $4";
        h = check_for_label (str);
        printf ("%d\n", h);
        return 0;
    }
    When I run it with the colon in the array, h = 5, and when there's is no colon, h = 0. But when I try to integrate the function into another program, instead of returning h = 0, it returns some random numbers, like 11824 etc instead of 0, but still returns correct h value if there's a colon. I understand that function strchr returns NULL if no matching character is found, so could this be the problem?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Right -- if there's no colon, then the for loop never happens, which means i never gets set. So you're returning whatever happens to be in i to start with. If you want 0 to be the answer, you should initialize i in check_for_label.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    int check_for_label (char str[])
    {
        char * pch;
        int i;
        for (pch = strchr (str, ':'); pch != NULL; pch = strchr (pch + 1,':'))
        {
            i = pch - str + 1;
        }
        return i;
    }
    i is actually not set to anything if pch == NULL in the first search. So you will return some "garbage" - such as 11834 or whatever happens to be in "i" when you return it without entering the for-loop at all (pch == NULL after first strchr()).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    67
    Great. I set i = 0 in check_for_label() and it all works. Thanks guys

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question about const pointers and const ints
    By WarDoGG in forum C Programming
    Replies: 9
    Last Post: 01-08-2011, 02:11 PM
  2. Array pointers question?
    By ben2000 in forum C Programming
    Replies: 4
    Last Post: 07-26-2007, 01:31 AM
  3. Help With Pointers
    By 98dodgeneondohc in forum C Programming
    Replies: 17
    Last Post: 06-12-2007, 02:34 PM
  4. Pointers to objects on the heap
    By foot in forum C++ Programming
    Replies: 3
    Last Post: 07-21-2005, 12:20 PM
  5. Array pointers to Multi-Dimensional Arrays
    By MethodMan in forum C Programming
    Replies: 3
    Last Post: 03-18-2003, 09:53 PM