Connect with us

Function and their types

Discussion in 'Microcontrollers, Programming and IoT' started by vead, Jan 21, 2018.

Scroll to continue with content
  1. (*steve*)

    (*steve*) ¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd Moderator

    25,482
    2,830
    Jan 21, 2010
    What does the * mean there?

    But what if that function didn't change the value. Could you pass a constant value then?
     
  2. (*steve*)

    (*steve*) ¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd Moderator

    25,482
    2,830
    Jan 21, 2010
    That is incorrect.
    Code:
    #include<stdio.h>
     
    void function(int a);
    int main(void)
    {
     int z=10;
     printf(" Before calling z = %d \n",z);
     function(z);
     printf("After calling z=%d \n",z);
     return 0;
    }
    void function(int *a)
    {
     a*=15;
    }
    
    Consider the example above.

    During execution, what does a hold?
     
  3. vead

    vead

    473
    14
    Nov 27, 2011
    int z; // we are declaring variable name z is name of variable //
    int z=10; // we are declaring variable and assigning value 10 to variable z

    &z means the address of variable z

    int *a // we are declaring pointer variable and name of pointer variable is a and * is operator //
    int *a = 15 // we are declaring and assigning value 15 to pointer variable a //

    int *a = &Z means we are assigning address of variable z to pointer variable a

    Code:
    #include<stdio.h>
     
    void function(int a);
    int main(void)
    {
     int z=10;
     printf(" Before calling z = %d \n",z);
     function(z);
     printf("After calling z=%d \n",z);
     return 0;
    }
    void function(int *a)
    {
     a*=15;
    }
    actual arguments is z=10; and formal arguments is a* = 15

    When program start to execute. it store the value of variable z = 10 then when we call function we pass actual value 10 to function this is known as function call by value, we are passing the copy of actually argument to function

    I think during execution a will hold the actual value 10
     
    Last edited: Jan 24, 2018
  4. (*steve*)

    (*steve*) ¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd Moderator

    25,482
    2,830
    Jan 21, 2010
    Aren't they the same thing? How can they be different?

    No. It can't hold 10, because a is not an integer variable.

    But we never do that, right?
     
  5. vead

    vead

    473
    14
    Nov 27, 2011
    int z = 10;
    int * a = 15;

    I think a is pointer variable and it store the address of variable 15
    so pointer variable store only the address of another variable. a will hold the address of z during the execution right

    int *a = 15
    we don't write this because compiler assign address to pointer variable right
     
  6. Harald Kapp

    Harald Kapp Moderator Moderator

    11,310
    2,588
    Nov 17, 2011
    There is no variable named 15.
    a is a pointer to an address that stores an integer.
    *a is the value stored at this adress. This value is 15.

    A pointer stores an address. This address need not necessarily be the address of a variable. It can also be the address of an I/O port, of a data structure or whatever.

    Syntactically this is o.k. You can do this, if you like and if you understand the correct use of pointers. But typically there is simply no use for using this declaration. If you need to declare an integer, use "int a=15".

    Generally pointers should be used with lots of care since it is very easy to create havoc using wrong pointer assignments. Incorrectly using pointers can destroy memory content in unexpected locations leading to totally unexpected and unpredictable program behavior.
     
    vead likes this.
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-