Thread: Still confused with pointers

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

    Still confused with pointers

    I was trying to return the position of a character in a string. The code below doesn't work because of the int *c, if I remove the *, it works fine. Just out of curiosity, how would it be possible to make it work without removing the "*" ? Whitch method would usually be prefered ?

    Code:
    int find_chr_index(char char *aeg, int *c)
    {
    	int p = strlen(aeg);
    	int i = 0;
    
    	for(i = 0; i < p; i++)
    	{
    		if((int)aeg[i] == c) return i;
    	}
    	return -1;
    }
    Also..how to compare and add a string and a char (given as char *a str and char c) ?

    Best wishes and thanks, D5

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Why are you looking for an integer pointer in a text string?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    30
    Quote Originally Posted by Dave_Sinkula View Post
    Why are you looking for an integer pointer in a text string?
    By a stupid mistake.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Your function looks a lot like the standard library function strchr. You could do something like this:
    Code:
       const char text[] = "hello world";
       const char *found = strchr(text, 'r');
       if ( found )
       {
          int pos = found - text;
          printf("pos = %d\n", pos);
       }
    But if I were to roll my own, it might look like this.
    Code:
    int mystrchr(const char *text, int c)
    {
       const char *s = text;
       while ( *s )
       {
          if ( *s == c )
          {
             return s - text;
          }
          ++s;
       }
       return -1;
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    That code doesn't do what you expect at all. And WTH is char char *? This function's prototype should be:

    Code:
    int find_chr_index(const char *aeg, const int c)
    There's no need to pass an int pointer to this function, and neither argument is to be modified within, so therefore you would qualify each as const.

    Perhaps running this code and observing the output might help:

    Code:
    #include <stdio.h>
    
    // Find the needle in the haystack.
    int find_chr_index(const char *haystack, const int needle)
    {
       if (NULL == haystack)
       {
          printf("Invalid argument\n");
          return -1;
       }
    
       printf("Memory address of variable 'haystack': 0x&#37;x\n", &haystack);
       printf("Memory address SAVED in variable 'haystack': 0x%x\n", haystack);
       printf("Value of *haystack (contents of memory address saved in variable 'haystack': %c\n", *haystack);
    
       // A convenience pointer, pointing to the start
       // of the string we wish to search.
       const char *p = haystack;
       
       printf("Memory address of variable 'p': 0x%x\n", &p);
       printf("Memory address SAVED in variable 'p': 0x%x\n", p);
       printf("Value of *p (contents of memory address saved in variable 'p'): %c\n\n", *p);
    
       // Our index counter.
       int i = 0;
    
       printf("Entering loop...\n");
    
       // We will stop when we reach the character desired,
       // or when we reach the NULL character that terminates
       // this string.
       while (NULL != p && '\0' != *p)
       {
          printf("\tMemory address of variable 'p': 0x%x\n", &p);
          printf("\tMemory address SAVED in variable 'p': 0x%x\n", p);
          printf("\tValue of *p (contents of memory address saved in variable p): %c\n\n", *p);
    
          // If the character pointed to by p is equivalent
          // to the character provided, we will return the value of our counter.
          if (*p == needle)
             return i;
    
          // Increment the address saved in variable 'p' by the size
          // of a character.
          ++p;
    
          // Increment our counter.
          ++i;
       }
    
       return -1;
    }
    
    int main(void)
    {
       const char *testString = "This is a string with a q in it";
       const char testChars[] = { 'q', 'g', 'z' };
       int result = -1;
       int i = 0;
       for (; i < (sizeof(testChars) / sizeof(testChars[0])); ++i)
       {
          char testChar = testChars[i];
          if (-1 == (result = find_chr_index(testString, testChar)))
             printf("Did not find character %c\n\n", testChar);
          else
             printf("Found character %c at position %d\n\n", testChar, result);
       }
    
       return 0;
    }
    Last edited by rags_to_riches; 02-22-2008 at 05:49 PM. Reason: Cleanup

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
       int i = 0;
       for (; i < (sizeof(testChars) / sizeof(testChars[0])); ++i)
       {
          char testChar = testChars[i];
    If you enabled compiler warnings, you would probably see that it complained about a signed/unsigned mismatch in the comparison of the for loop, because i is a signed int and sizeof() evaluates to an unsigned integer (a size_t, actually). I'd make i a size_t -- or maybe use a pointer instead.
    Code:
    for(p = testChars; *p; p ++)
    Declaring variables in the middle of blocks, such as find_chr_index::p and find_chr_index::i, is C99.

    Also:
    Code:
    printf("\tMemory address of variable 'p': 0x&#37;x\n", &p);
    There is a %p for that very reason. GCC wants you to cast the parameter to (void *), but I'm not sure if that's entirely necessary or not.
    Code:
    printf("\tMemory address of variable 'p': %p\n", &p);
    Good code, though . . . nicely commented.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by dwks View Post
    Code:
    printf("\tMemory address of variable 'p': 0x%x\n", &p);
    There is a %p for that very reason. GCC wants you to cast the parameter to (void *), but I'm not sure if that's entirely necessary or not.
    Code:
    printf("\tMemory address of variable 'p': %p\n", &p);
    C99 says that %p is for a void pointer.
    Code:
    printf("\tMemory address of variable 'p': %p\n", (void *) &p);

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Dave_Sinkula View Post
    But if I were to roll my own, it might look like this.
    Code:
    int mystrchr(const char *text, int c)
    {
       const char *s = text;
       while ( *s )
       {
          if ( *s == c )
          {
             return s - text;
          }
          ++s;
       }
       return -1;
    }
    Why int c instead of char c?

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by cpjust View Post
    Why int c instead of char c?
    Signature equivalence with strchr.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Hey guys..need help on pointers
    By Darkozuma in forum C++ Programming
    Replies: 5
    Last Post: 07-25-2008, 02:57 PM
  3. function pointers
    By benhaldor in forum C Programming
    Replies: 4
    Last Post: 08-19-2007, 10:56 AM
  4. confused with pointers
    By Mahdi123 in forum C Programming
    Replies: 2
    Last Post: 04-25-2007, 01:08 PM
  5. Replies: 4
    Last Post: 12-10-2006, 07:08 PM