Thread: to pass address from char to pointer VS to pass address from integer to pointer

  1. #1
    Registered User
    Join Date
    Aug 2015
    Posts
    5

    Question to pass address from char to pointer VS to pass address from integer to pointer

    Hi guys,

    Whenever I pass an adress from interger variable to a pointer (a function argument) I need to put '&' command, but when I pass an address from char variable to a pointer(function argument too) I don't need to put '&'. Why does it happen?

    Code:
    char text[3] = "hi ";
    int v = 0;
    
    void func1(char *str);
    void func2(int *x);
    
    void main()
    {
       func1(text);
       func2(&v);
    }
    
    void func1(char *str)
    {
    while(*str==' ') {
    *str='!';
    }
    } void func2(int *x) {
    *x = 1;
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You're not passing a single character to the function, you're passing a character array. Arrays are passed to functions as pointers, and an array name by itself acts as a pointer to the first element in this context. Hence, since you're already passing a pointer, you don't need the &.

    Question 6.4

    Note that this does not mean pointers and arrays are the same - they just behave similarly sometimes:

    Question 6.3

    Also:


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How come char pointer doesn't need an address sign?
    By yj1214 in forum C Programming
    Replies: 8
    Last Post: 08-11-2015, 02:17 AM
  2. Replies: 4
    Last Post: 02-14-2012, 07:45 PM
  3. Pass by Reference with a pointer to char
    By JoshNCSU22 in forum C Programming
    Replies: 16
    Last Post: 10-10-2008, 11:28 AM
  4. Pass by reference vs pass by pointer
    By Non@pp in forum C++ Programming
    Replies: 10
    Last Post: 02-21-2006, 01:06 PM
  5. Should i pass address of pointer or just pointer???
    By howhy in forum C++ Programming
    Replies: 11
    Last Post: 09-02-2005, 04:05 AM