•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 332,687 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,439 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: 5793 | Replies: 4
![]() |
•
•
Join Date: Sep 2004
Posts: 1
Reputation:
Rep Power: 0
Solved Threads: 0
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);
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);
>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.
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.
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.
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.
>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:
There's really no need to declare a pointer just for the purpose of passing it to a function. This works just as well:
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.
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:
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.
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);
}int main ( void )
{
int a = 23;
show_value ( &a );
}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);
}int main ( void )
{
char string[] = "BOSS";
show_string ( string );
} Member of: Beautiful Code Club.
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
Similar Threads
- what is pointer to function ??? (C)
- pointer to function (C)
- Passing char * to function and populating inside (C++)
- C++ pass 2d array into function (C++)
Other Threads in the C Forum
- Previous Thread: Estimate value of e
- Next Thread: End of file function



Linear Mode