c language problm, how to pass pointer to a function

Reply

Join Date: Sep 2004
Posts: 1
Reputation: hemant_is_here is an unknown quantity at this point 
Solved Threads: 0
hemant_is_here hemant_is_here is offline Offline
Newbie Poster

c language problm, how to pass pointer to a function

 
0
  #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);
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,566
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 705
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

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

 
0
  #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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 33
Reputation: letmec is an unknown quantity at this point 
Solved Threads: 2
letmec's Avatar
letmec letmec is offline Offline
Light Poster

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

 
0
  #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 Quick reply to this message  
Join Date: Sep 2004
Posts: 33
Reputation: letmec is an unknown quantity at this point 
Solved Threads: 2
letmec's Avatar
letmec letmec is offline Offline
Light Poster

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

 
0
  #4
Oct 3rd, 2004
let me help you
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,566
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 705
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

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

 
0
  #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:
  1. main()
  2. {
  3. int a=23,*add_of_a;
  4. add_of_a=&a;
  5. show_value(add_of_a);
  6. }
There's really no need to declare a pointer just for the purpose of passing it to a function. This works just as well:
  1. int main ( void )
  2. {
  3. int a = 23;
  4.  
  5. show_value ( &a );
  6. }
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.
  1. main()
  2. {
  3.  
  4. char STRING[]="BOSS";
  5. char *add_of_first_element_of_arr;
  6. add_of_first_element_of_arr=&STRING[0];
  7.  
  8. show_string(add_of_first_element_of_arr);
  9. }
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:
  1. int main ( void )
  2. {
  3. char string[] = "BOSS";
  4.  
  5. show_string ( string );
  6. }
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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 51
Reputation: nateuni has a little shameless behaviour in the past 
Solved Threads: 0
nateuni nateuni is offline Offline
Junior Poster in Training

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

 
0
  #6
Sep 18th, 2009
Originally Posted by Narue View Post
>

>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
.
I know you hate me Narue,. but that site just solved something that I was really struggling with!

Thanks buddy that site was gold.

xo
Nate
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,818
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 296
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is online now Online
Roasting Maven

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

 
0
  #7
Sep 18th, 2009
Originally Posted by nateuni View Post
I know you hate me Narue,.
What the hell are you talking about?
And why are you posting this in a FIVE year old thread?
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 51
Reputation: nateuni has a little shameless behaviour in the past 
Solved Threads: 0
nateuni nateuni is offline Offline
Junior Poster in Training

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

 
0
  #8
Sep 18th, 2009
Originally Posted by niek_e View Post
What the hell are you talking about?
And why are you posting this in a FIVE year old thread?
To reply to the hate comment there was regarding a little beef that Narue and I recently had in another thread.

And c is how many decades old.. so why should syntax related threads date?

I was thanking Narue there for her post (regardless of date) as it saved me a LOT of messing about.

Just giving thanks where thanks is due!
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,818
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 296
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is online now Online
Roasting Maven

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

 
0
  #9
Sep 18th, 2009
Although bumping threads is not against the rules here on Daniweb, it is still frowned upon. Especially when it comes to "I agree" posts.
Just leave the thread dead and buried where it was.

With that said, let me now frown upon you:
Last edited by niek_e; Sep 18th, 2009 at 9:16 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 51
Reputation: nateuni has a little shameless behaviour in the past 
Solved Threads: 0
nateuni nateuni is offline Offline
Junior Poster in Training

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

 
0
  #10
Sep 18th, 2009
Originally Posted by niek_e View Post
Although bumping threads is not against the rules here on Daniweb, it is still frowned upon. Especially when it comes to "I agree" posts.
Just leave the thread dead and buried where it was.

With that said, let me now frown upon you:
Ok my bad.. ! My bad,.. seems like I am stepping on toes on over the place on here.. and not meaning too.

I just want to be a good boy!
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC