943,617 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 39829
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 1st, 2004
0

c language problm, how to pass pointer to a function

Expand Post »
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



  1. #include<stdio.h>
  2. int main()
  3. {
  4. int **a,r,c,i,j,re;
  5. printf("enter dimensions of array rows and columns ");
  6. scanf("%d%d",&r,&c);
  7. a = (int**)malloc(r*sizeof(int*));
  8. for(i = 0;i <=r-1;i++)
  9. {a[i] = (int*)malloc(c*sizeof(int));
  10. if(a == NULL||a[i] == NULL)
  11. printf("memory not allocated ");}
  12. for(i=0;i<=r-1;i++)
  13. {
  14. for(j=0;j<=c-1;j++)
  15. {printf("enter element a_%d%d\t",i,j);
  16. scanf("%d",&a[i][j]);
  17. printf("%s\n",&a[i][j]);
  18. }
  19. printf("\n");
  20. }
  21. re = func(a,r,c);
  22. if(re == 1)
  23. printf("1");
  24. else
  25. printf("0");
  26. return(0);
  27. }
  28. func(int *ptr,int row,int col)
  29. {
  30. int i,j,num,*k,count=0;
  31. printf("enter number ");
  32. scanf("%d",&num);
  33. for(i=0;i<=row-1;i++)
  34. {
  35. for(j=0;j<=col-1;j++)
  36. {
  37. k = (ptr + i*col + j);
  38. if(num ==*k);
  39. {
  40. printf("num = %d\tk =%d %d\t*k = %d ",num,k,(ptr + i*col + j),*k);
  41. printf("\nnum %d found in %d th column of %d th row at position%d\n",num,i,j,i*col+j+1);
  42. count+=1;
  43. }
  44. }
  45. }
  46. if(count!=0)
  47. return(1);
  48. else
  49. return(0);
Last edited by Nick Evan; Feb 11th, 2010 at 11:52 am. Reason: Added code-tags
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hemant_is_here is offline Offline
1 posts
since Sep 2004
Oct 1st, 2004
0

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

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 3rd, 2004
0

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

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:

  1. main()
  2. {
  3. int a=23,*add_of_a;
  4. add_of_a=&a;
  5. show_value(add_of_a);
  6. }
  7.  
  8. void show_value(int *add)
  9. {
  10. printf("\nThe Value of a = %d",add);
  11. }

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:

  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. }
  10.  
  11. void show_string(char *add)
  12. {
  13. int i=0;
  14. while(add[i]!='\0')
  15. {
  16. printf("%c",add[i]);
  17. i++;
  18. }
  19. }
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.
Last edited by Nick Evan; Feb 11th, 2010 at 11:52 am.
Reputation Points: 10
Solved Threads: 2
Light Poster
letmec is offline Offline
33 posts
since Sep 2004
Oct 3rd, 2004
0

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

let me help you
Reputation Points: 10
Solved Threads: 2
Light Poster
letmec is offline Offline
33 posts
since Sep 2004
Oct 3rd, 2004
0

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

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Sep 18th, 2009
0

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

Click to Expand / Collapse  Quote originally posted by Narue ...
>

>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
Reputation Points: 20
Solved Threads: 0
Junior Poster in Training
nateuni is offline Offline
61 posts
since Aug 2009
Sep 18th, 2009
0

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

Click to Expand / Collapse  Quote originally posted by nateuni ...
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?
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Sep 18th, 2009
0

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

Click to Expand / Collapse  Quote originally posted by niek_e ...
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!
Reputation Points: 20
Solved Threads: 0
Junior Poster in Training
nateuni is offline Offline
61 posts
since Aug 2009
Sep 18th, 2009
0

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

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 Nick Evan; Sep 18th, 2009 at 9:16 am.
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Sep 18th, 2009
0

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

Click to Expand / Collapse  Quote originally posted by niek_e ...
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!
Reputation Points: 20
Solved Threads: 0
Junior Poster in Training
nateuni is offline Offline
61 posts
since Aug 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in C Forum Timeline: jpeg image compression in cuda
Next Thread in C Forum Timeline: i want to make factorial program with IF condition





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC