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 374,179 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 3,456 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:
Views: 6633 | Replies: 4
Reply
Join Date: Sep 2004
Posts: 1
Reputation: hemant_is_here is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
hemant_is_here hemant_is_here is offline Offline
Newbie Poster

c language problm, how to pass pointer to a function

  #1  
Oct 1st, 2004
hi all
plz explain me with syntax "how to pass pointer to a function or pointers to a function " . plz tell me if there any big tutorial which can solve my problem providing a deep knowledge about it .
and one more probim :
what is incorrect int following program. this is a program to search a num in an array whose dimension and numbers are specified by user it is not running properly



#include<stdio.h>
int main()
{
int **a,r,c,i,j,re;
printf("enter dimensions of array rows and columns ");
scanf("%d%d",&r,&c);
a = (int**)malloc(r*sizeof(int*));
for(i = 0;i <=r-1;i++)
{a[i] = (int*)malloc(c*sizeof(int));
if(a == NULL||a[i] == NULL)
printf("memory not allocated ");}
for(i=0;i<=r-1;i++)
{
for(j=0;j<=c-1;j++)
{printf("enter element a_%d%d\t",i,j);
scanf("%d",&a[i][j]);
printf("%s\n",&a[i][j]);
}
printf("\n");
}
re = func(a,r,c);
if(re == 1)
printf("1");
else
printf("0");
return(0);
}
func(int *ptr,int row,int col)
{
int i,j,num,*k,count=0;
printf("enter number ");
scanf("%d",&num);
for(i=0;i<=row-1;i++)
{
for(j=0;j<=col-1;j++)
{
k = (ptr + i*col + j);
if(num ==*k);
{
printf("num = %d\tk =%d %d\t*k = %d ",num,k,(ptr + i*col + j),*k);
printf("\nnum %d found in %d th column of %d th row at position%d\n",num,i,j,i*col+j+1);
count+=1;
}
}
}
if(count!=0)
return(1);
else
return(0);
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2004
Posts: 6,005
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 26
Solved Threads: 409
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

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

  #2  
Oct 1st, 2004
>plz explain me with syntax "how to pass pointer to a function or pointers to a function "
For each level of indirection in the function argument list, add an asterisk. If the argument isn't already a pointer of the same type as the argument list, dereference or add & as necessary.

>plz tell me if there any big tutorial which can solve my problem providing a deep knowledge about it
http://pweb.netcom.com/~tjensen/ptr/pointers.htm

>what is incorrect int following program.
A lot, actually. First and foremost you forgot to include stdlib.h, an error that the casting of malloc's return value hid and is a good example of why you don't cast the return of malloc. Second, you don't provide a prototype for func even though the definition is after the first call. a is a pointer to a pointer to int yet func expects a pointer to int so that's a type mismatch, you test for null pointers after dereferencing the allocated memory so the damage would already have been done by that point. All in all, the code could be improved greatly, but I'll leave that to you after you look at the link I gave you.
Member of: Beautiful Code Club.
Reply With Quote  
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  
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

  #4  
Oct 3rd, 2004
let me help you
Reply With Quote  
Join Date: Sep 2004
Posts: 6,005
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 26
Solved Threads: 409
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

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

  #5  
Oct 3rd, 2004
>What, if we want to pass more than one value through one argument of a function, here is place where pointers comes.
That's a pretty roundabout way of saying that you can pass an array as a function argument. :rolleyes:
main()
{
int a=23,*add_of_a;
add_of_a=&a;
show_value(add_of_a);
}
There's really no need to declare a pointer just for the purpose of passing it to a function. This works just as well:
int main ( void )
{
  int a = 23;

  show_value ( &a );
}
But the example is silly as there's no point in passing a as a pointer. The two primary reasons for passing a pointer to T is so that the original object of T can be changed within the function, which is a great boon for modularization, or if T is so large that passing it by value would end up copying too much data, in which case a pointer to T is smaller and more efficient. Your example has neither of those attributes.
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);
}
Once again, there's no point in saving the address of the first element of the array in a pointer. This conversion is done implicitly anytime an array name is used in value context, such as when it is the argument to a function:
int main ( void )
{
  char string[] = "BOSS";

  show_string ( string );
}
Because the array name is converted to a pointer, there's no need to take its address as in the previous example using a single integer.
Member of: Beautiful Code Club.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 4:34 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC