Like Tree1Likes
  • 1 Post By quzah

Passing address or pointer as argument to function

This is a discussion on Passing address or pointer as argument to function within the C Programming forums, part of the General Programming Boards category; Code: char_t buffer[10]; somefunction(&buffer); Code: char_t* pbuffer; pbuffer = (char_t*)malloc(10); somefunction(pbuffer); somefunction will be filling up the buffer with data ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    31

    Passing address or pointer as argument to function

    Code:
    char_t buffer[10];
    
    somefunction(&buffer);
    Code:
    char_t* pbuffer;
    pbuffer = (char_t*)malloc(10);
    
    somefunction(pbuffer);
    somefunction will be filling up the buffer with data

    Is there any difference between the above 2? From what I understand there is no difference since both are passing a pointer to the function. Am I right?

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Yes, the first one is wrong. Arrays decay to pointers when passed to functions so you can drop the address of operator, e.g.
    Code:
    char_t buffer[10];
    somefunction(buffer);
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    31
    oh so if we want to pass a char array into a function, we just have to pass in the name of the array? like in your example?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Edelweiss View Post
    like in your example?
    No, we just like to make up stuff here.


    Quzah.
    Edelweiss likes this.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Yes, when you pass the name of the array, it decays into a pointer to the first element. So,

    Code:
    void foo(char*);
    
    int main(void){
    
         char myArray[]={'a','b','c'};
         foo(myArray);
         return (0);
    }
    void foo(char* array){
         char q;
         q=array[1];
         printf("%c", q); //this will print b
    }
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  6. #6
    Registered User
    Join Date
    Aug 2011
    Posts
    31
    Quote Originally Posted by quzah View Post
    No, we just like to make up stuff here.


    Quzah.
    lolol

  7. #7
    Registered User
    Join Date
    Aug 2011
    Posts
    31
    Thanks!

  8. #8
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by quzah View Post
    No, we just like to make up stuff here.
    Quzah.
    Haha.....well sometimes we do, mostly when I post though.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 05-24-2011, 05:57 AM
  2. Replies: 5
    Last Post: 03-06-2011, 10:57 AM
  3. Passing Argument from incompatible pointer type
    By AmritxD in forum C Programming
    Replies: 3
    Last Post: 08-15-2010, 03:23 PM
  4. passing argument from incompatible pointer type
    By bhdavis1978 in forum C Programming
    Replies: 5
    Last Post: 03-17-2010, 12:42 PM
  5. passing pointer argument
    By timhxf in forum C Programming
    Replies: 8
    Last Post: 01-08-2007, 09:38 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21