Thread: Pass by reference a character pointer in c

  1. #1
    Registered User
    Join Date
    Jun 2009
    Location
    Bangalore,India
    Posts
    29

    Pass by reference a character pointer in c

    Code:
    int mystrlen(const char *str)
    {
    	int len = 0;
    	while (*str++)
    		len++;
    	return len;
    }
    
    
    void main() {
    
    	char *str1 = "Hello";
    
    	printf("\n length = %d \n", mystrlen(str1));
    	printf(" str=%s\n", str1);
    }
    o/p :
    length = 5
    str=Hello

    Here in main function, we call mystrlen() and pass a reference address of str.

    In function mystrlen(), we modify/increase(str++) the string pointer till '\0'.
    In main() we print the str after function call mystrlen. But string will print whole contents. It will print hello. How is this possible? Because in mystrlen() the pointer pointing to NUL...

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    How is this possible?
    It's happening because you're passing the pointer by value, so you are changing a copy, not the original pointer.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Main returns int, not void.
    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.

  4. #4
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    277
    Quote Originally Posted by krishnampkkm View Post
    Code:
    int mystrlen(const char *str)
    {
        int len = 0;
        while (*str++)
            len++;
        return len;
    }
    
    
    void main() {
    
        char *str1 = "Hello";
    
        printf("\n length = %d \n", mystrlen(str1));
        printf(" str=%s\n", str1);
    }
    o/p :
    length = 5
    str=Hello

    Here in main function, we call mystrlen() and pass a reference address of str.

    In function mystrlen(), we modify/increase(str++) the string pointer till '\0'.
    In main() we print the str after function call mystrlen. But string will print whole contents. It will print hello. How is this possible? Because in mystrlen() the pointer pointing to NUL...
    In C everything gets passed by value. But! You can emulate any level of references using pointers. A pointer gives you a reference to a value, a double pointer a reference to a reference to a value, and so on.

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    void skip_space(char** s)
    {
        
        while(isspace(**s))
            (*s)++;
    }
    int main(void)
    {
        char * spacey = "                 <- space starts here";
        skip_space(&spacey);
        puts(spacey);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-14-2012, 07:45 PM
  2. Pass by Reference with a pointer to char
    By JoshNCSU22 in forum C Programming
    Replies: 16
    Last Post: 10-10-2008, 11:28 AM
  3. Pass by reference vs pass by pointer
    By Non@pp in forum C++ Programming
    Replies: 10
    Last Post: 02-21-2006, 01:06 PM
  4. Pass by pointer, reference, value.
    By Lithorien in forum C++ Programming
    Replies: 8
    Last Post: 02-25-2005, 10:03 AM
  5. passing character array as pass by reference?
    By agerealm in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2002, 11:16 PM

Tags for this Thread