Thread: What I 'm passing is address or value

  1. #1
    Registered User
    Join Date
    Oct 2022
    Posts
    95

    What I 'm passing is address or value

    I don't understand What I 'm passing is address or value in following code

    Code:
     #include<stdio.h>
    
    void foo ( int p )  
    {
        printf(" %d", p);
    }
     
    int main(void) {
      
      int array[] = {1, 2, 3, 4, 5};
      
       foo(array);
        
        return 0;
    }
    I am passing address or value where it is written in main function or other function .

    which line 3 or 12 define What I 'm passing is address or value ?

    I believe line 12 pass something to function foo from main function
    Last edited by Kittu20; 12-14-2022 at 02:07 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    Well neither at the moment, since it doesn't compile.
    Code:
    $ gcc foo.c
    foo.c: In function ‘main’:
    foo.c:12:8: warning: passing argument 1 of ‘foo’ makes integer from pointer without a cast [-Wint-conversion]
       12 |    foo(array);
          |        ^~~~~
          |        |
          |        int *
    foo.c:3:16: note: expected ‘int’ but argument is of type ‘int *’
        3 | void foo ( int p )
          |            ~~~~^
    Consider one of

    An address.
    Code:
    #include<stdio.h>
    
    void foo ( int *p )  
    {
        printf(" %d", *p);
    }
     
    int main(void) {
      
      int array[] = {1, 2, 3, 4, 5};
      
       foo(array);
        
        return 0;
    }
    A value.
    Code:
    #include<stdio.h>
    
    void foo ( int p )  
    {
        printf(" %d", p);
    }
     
    int main(void) {
      
      int array[] = {1, 2, 3, 4, 5};
      
       foo(array[0]);
        
        return 0;
    }
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing by Address
    By jaaaaaamie in forum C Programming
    Replies: 2
    Last Post: 08-22-2013, 04:48 PM
  2. Passing a structure by address
    By Sharke in forum C Programming
    Replies: 2
    Last Post: 03-24-2009, 12:43 AM
  3. Passing by refernce of address
    By lruc in forum C++ Programming
    Replies: 14
    Last Post: 09-03-2008, 02:21 AM
  4. strlen() and passing by address
    By supaben34 in forum C++ Programming
    Replies: 2
    Last Post: 07-12-2003, 01:49 PM
  5. passing by address vs passing by reference
    By lambs4 in forum C++ Programming
    Replies: 16
    Last Post: 01-09-2003, 01:25 AM

Tags for this Thread