Thread: Address off when returning pointer from function.

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    19

    Address off when returning pointer from function.

    Hello,

    I am writing a program for the problem: Write a function that receives a string, and returns a pointer to the first non space character in the string.

    I could do it more simply by just passing a pointer to the string and changing that but I want to get it to work the way the problem is stated. Here is the code I have written:

    Code:
    #include <stdio.h>
    
    char *space_finder(char strng[]);	/* Function prototype for finding the first string in a character */
    
    int main(){
    
    	char test_string[] = "Where is the first space?";  /* test string */
    
    	char *return_ptr;	/*pointer for the pointer space_finder returns */
    
    	return_ptr = space_finder(test_string);	/* send test string to space_finder function and assign returned pointer to return_ptr */
    
    
    	/* test to see if the return_ptr equals the first char of the array(empty string) or if it is the end of string character(no spaces) */
    
    	if((return_ptr == test_string)||(*return_ptr== '\0')){
    
    		printf("\n\nNo spaces were in the sentence.\n\n");
    
    	}else{
    
    	printf("The first character after the space is \n%c\n%c\n%c\n", *(return_ptr-1),*(return_ptr),*(return_ptr+1));
    	}
    
    return(0);
    
    }
    
    char *space_finder(char strng[]){
    
    
    
    	char *ptr; 	/* temporary local pointer */
    	int i;		/* loop control variable */
    	int loopmax;	/* loop max size */
    	
    	ptr = strng;	/* point ptr to first address of strng */
    
    	/* Loop control uses a -1 to account for the null character*/
    	loopmax = (sizeof(strng)/sizeof(char))-1;
    	for(i=0;i<loopmax;i++){
    	
    		if(*(ptr+i)== ' '){
    			
    			/* printf is for debugging and showing what the characters are that border the current pointer value(ptr+1) */
    			printf("\n\nInside If:\n\n%c\n%c\n%c\n\n",*(ptr+i-1),*(ptr+i),*(ptr+i+1));
    			/* violate loop enter conditions */
    			i=loopmax;
    			ptr_return = (ptr+i);
    			
    			return ptr_return;
    		}else if(*(ptr+i)== '\0'){
    			/* return the null pointer to main */
    			return ptr+i;
    		}
    	
    
    	}
    	
    	
    }
    When I run the code I get the output:

    Code:
    Inside If:
    
    e
     
    i
    
    The first character after the space is 
    i
    s

    This tells me that inside the if statement the pointer is correctly pointing to the first space. When the pointer is returned to main though, it is pointing to the second character after the first space.

    If anyone can offer insight into where I am going wrong I would appreciate it. I am doing this for giggles so I am mostly interested in the why as I want to get a good grasp of pointers.

    Thank you for reading this!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > loopmax = (sizeof(strng)/sizeof(char))-1;
    Use strlen(strng), not sizeof.

    The sizeof thing only works when the array declaration is in scope. Passing the array to a function reduces to a single pointer with no size information about the original array (except for it's type).

    Edit
    $ gcc -Wall bar.c
    bar.c: In function ‘space_finder’:
    bar.c:61: warning: control reaches end of non-void function
    Also consider what happens if you should fall out of the loop without detecting anything.
    It might not be a specific problem in this code, but in general, falling off the end of a non-void function is bad.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    char *FindSpace(const char *str)
      { char *ip =  str;          // internal pointer
         while(*ip++)              // search to end of string
           if (*ip == ' ')            // found a space
             return ip;              //  return the pointer
          return NULL; }         // string has no spaces
    Last edited by CommonTater; 09-14-2011 at 03:13 PM. Reason: oopsies!

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    19

    Thanks

    Thank you both for your replies.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing address or pointer as argument to function
    By Edelweiss in forum C Programming
    Replies: 7
    Last Post: 08-17-2011, 12:38 AM
  2. Returning pointer from function question
    By Rune Hunter in forum C++ Programming
    Replies: 12
    Last Post: 07-12-2007, 09:45 AM
  3. Returning address of Function. Pointers to Functions.
    By edunia11 in forum C Programming
    Replies: 2
    Last Post: 12-04-2006, 12:28 PM
  4. function returning pointer
    By blue_gene in forum C Programming
    Replies: 7
    Last Post: 04-19-2004, 02:35 PM