User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 361,941 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,647 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser:

c language problm, how to pass pointer to a function

Join Date: Sep 2004
Posts: 32
Reputation: letmec is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 2
letmec's Avatar
letmec letmec is offline Offline
Light Poster

Re: c language problm, how to pass pointer to a function

  #3  
Oct 3rd, 2004
As you know that we can pass one value at a time through one argument of any function. What, if we want to pass more than one value through one argument of a function, here is place where pointers comes.

pointers stores addresses, and we pass address to a function we can access it's value. So the general definition to pass a pointer to a function is:
return_type Function_name(date_type_of_the_pointer);

For Example:

main()
{
int a=23,*add_of_a;
add_of_a=&a;
show_value(add_of_a);
}

void show_value(int *add)
{
printf("\nThe Value of a = %d",add);
}


Now you want to pass many pointers then you can define array of pointers
as follows:

Data_type *Arr_of_pointers_name[SIZE];

Now as you have passed the address of single value, here also you have to pass only one value, i.e. the address of the first element of the array of pointers.

For Example:

main()
{

char STRING[]="BOSS";
char *add_of_first_element_of_arr;
add_of_first_element_of_arr=&STRING[0];

show_string(add_of_first_element_of_arr);
}

void show_string(char *add)
{
int i=0;
while(add[i]!='\0')
{
printf("%c",add[i]);
i++;
}
}

Here in the show_string() func we have passed the address of STRING[0],And we collected it in add. By add variable now we can access the whole string just by incrementing the value if i. Or we can print the whole string just by puting this printf() statement printf("%s",add) in the show_string() func.
Reply With Quote  
All times are GMT -4. The time now is 11:31 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC