• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

C and java passing paramter to a method

 
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi..

i just need to confirm is my thinking is correct or not.
java and c use the same concept that when passing a variable into a method is pass by copy(pass a copy of a pointer),because i CAN:T change the pointer to variable int a in this below code.


void test(int *b){
printf("value of b now is %d\n",*b);
printf("address of b(a) is : %d\n ",b);

/* try to change the pointer of variable a */
b = 0;
printf("address of b(a) now is : %d\n ",b);

}

int main(){
int a = 19;
printf("address of a is %d \n",&a);
// pass the address of a
test(&a);

// still pointing to the original address
printf("address of a after trying to change in test() method is %d\n",&a);

}



thanks
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java always uses "pass by value". C can use either "pass by value" or "pass by reference". For more information, please see this tutorial.
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can also check out this link.
reply
    Bookmark Topic Watch Topic
  • New Topic