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);

Edited 6 Years Ago by Nick Evan: Added code-tags

>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.

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;

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.

Edited 6 Years Ago by Nick Evan: n/a

>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.

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?

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!

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: :icon_frown:

Edited 7 Years Ago by Nick Evan: n/a

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: :icon_frown:

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!

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!

can u hlp me about modulu and looping statement?

If you can clearly state what inof you are looking for I can try and help you.

Regards,
Nate

Edited 3 Years Ago by pritaeas: Fixed formatting.

can u hlp me about modulu and looping statement?

This old thread has been accidently revived. Stop posting on this thread (I know this is rather hypocritical of me).

Start a new thread and for crying out loud, formulate a proper question. Your rather vague question has no relation to the topic of the original thread in any case!!

Edited 7 Years Ago by yellowSnow: n/a

Comments
Yes

This old thread has been accidently revived. Stop posting on this thread (I know this is rather hypocritical of me).

Start a new thread and for crying out loud, formulate a proper question. Your rather vague question has no relation to the topic of the original thread in any case!!

Yep,.. What snow said! No more posting I promise(after this one)!

Comments
no
#include<stdio.h>
#include<conio.h>
void display (int *j);
void main()
{
int *a;
int b=5;
a=&b;
display(a);
getch();
}
void display(int *j)
{
printf("%d",*j);
}

output........... 5

Edited 6 Years Ago by Nick Evan: Added code-tags

Comments
You forgot formatting, Salem
Years too late, non standard code, no code tags - fail fail fail
This article has been dead for over six months. Start a new discussion instead.