By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,510 Members | 1,815 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,510 IT Pros & Developers. It's quick & easy.

Searching an 2d array using pointers

P: 5
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
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<stdlib.h>
  4. #define N 4
  5. void find_key(int *,int,int);
  6.  
  7. void main(){
  8.     clrscr();
  9.     srand((unsigned)time(NULL));
  10.  
  11.     int arr[N][N];
  12.     int *p;
  13.         int key=32;
  14.         int len=16;
  15.     int count=0;
  16.     for(p=&arr[0][0];p<=&arr[N-1][N-1];p++){
  17.         if(count==4){
  18.             *p=32;
  19.         }
  20.         else{
  21.         *p=rand()%10+1;
  22.     }
  23.         count++;
  24.     }
  25.     for(p=&arr[0][0];p<=&arr[N-1][N-1];p++)
  26.         printf("\n%d",*p);
  27.  
  28.     find_key(&arr[0][0],len,key);
  29.     getch();
  30. }
  31. void find_temp(int *a,int n,int k){
  32.     int *p;
  33.     for(p=a;p<a+n;p++){
  34.         if(*p==k)
  35.             printf("\nfound key at %d",p);
  36.         else
  37.             printf("\nnot foumd");
  38.     }
  39. }
Sep 27 '10 #1
Share this Question
Share on Google+
4 Replies


ashitpro
Expert 100+
P: 542
Look at your line 31

Expand|Select|Wrap|Line Numbers
  1. void find_temp(int *a,int n,int k){
Change the function name to 'find_key', cause that's what you declared above 'main'.

Expand|Select|Wrap|Line Numbers
  1. void find_key(int *a,int n,int k){
See if that works...
Sep 27 '10 #2

P: 5
@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
Sep 27 '10 #3

P: 5
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??

Expand|Select|Wrap|Line Numbers
  1. for(p=arr[i];p<=arr[i]+N;p++){
  2.         printf("\n%d",*p);
Sep 27 '10 #4

Banfa
Expert Mod 5K+
P: 8,916
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.
Sep 27 '10 #5

Post your reply

Sign in to post your reply or Sign up for a free account.