Searching an 2d array using pointers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • touches
    New Member
    • Sep 2010
    • 5

    #1

    Searching an 2d array using pointers

    I am using an 2d array to search for the key 32 using pointers.. Now whenever i try to compile i get an linker error saying undefined symbol find_key(int *far,int,int)..
    I know the error is with the use of pointers and the way i am passing them... Can anyone help me out
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    #define N 4
    void find_key(int *,int,int);
    
    void main(){
    	clrscr();
    	srand((unsigned)time(NULL));
    
    	int arr[N][N];
    	int *p;
            int key=32;
            int len=16;
    	int count=0;
    	for(p=&arr[0][0];p<=&arr[N-1][N-1];p++){
    		if(count==4){
    			*p=32;
    		}
    		else{
    		*p=rand()%10+1;
    	}
    		count++;
    	}
    	for(p=&arr[0][0];p<=&arr[N-1][N-1];p++)
    		printf("\n%d",*p);
    
    	find_key(&arr[0][0],len,key);
    	getch();
    }
    void find_temp(int *a,int n,int k){
    	int *p;
    	for(p=a;p<a+n;p++){
    		if(*p==k)
    			printf("\nfound key at %d",p);
    		else
    			printf("\nnot foumd");
    	}
    }
  • ashitpro
    Recognized Expert Contributor
    • Aug 2007
    • 542

    #2
    Look at your line 31

    Code:
    void find_temp(int *a,int n,int k){
    Change the function name to 'find_key', cause that's what you declared above 'main'.

    Code:
    void find_key(int *a,int n,int k){
    See if that works...

    Comment

    • touches
      New Member
      • Sep 2010
      • 5

      #3
      @ashitpro
      Oh yeah it works sorry silly me... i didnt check with the function names..
      anyways i'll experiment with different pointer notations of passing an argument.. and if i'm stuck i'll post it here

      anyways thanks a lot

      Comment

      • touches
        New Member
        • Sep 2010
        • 5

        #4
        Now in the above code if i want to process only the rows.. this code works fine.. if i want to send each row one by one to the function to find the key can i pass the first array element arr[0] like find_key(arr[i],len,key) in the function call?? how would i write a function prototype for that?? I want to send each row of the 2d array one by one to the function to find the key
        I am really stuck as i could not understand parameter passing of pointers in 2d arrays to functions?? Can anyone explain that concept please??

        Code:
        for(p=arr[i];p<=arr[i]+N;p++){
                printf("\n%d",*p);

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          The way you are current declaring and calling find_key is not, in my opinion, a good design.

          You might want to start by reading our arrays revealed tutorial.

          Comment

          Working...