Thread: Searching an array with pointers

  1. #16
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The array and the pointer are both problems. If you pass an array to a function, just pass the name of the array, and nothing more. Like so:
    Code:
    int arrayfunction( int array[] )
    {
        ...blah...
    }
    
    int main( void )
    {
        int arrayofstuff[] = { 1, 2, 3, 4, 5, 6, 7 };
    
        arrayfunction( arrayofstuff );
    
        return 0;
    }
    Then see my previous posts for how to pass an integer or a pointer.

    Quzah.
    Hope is the first step on the road to disappointment.

  2. #17
    Registered User
    Join Date
    Feb 2004
    Posts
    73
    O, thats all it was. Ok, the [] just needed to be removed. Got it. Ill post the code in a sec. Beautiful. Thanks Dave


    final code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define TRUE 1
    #define FALSE 0
    #define N 10
    int search (int a [], int n, int key){
    int b;
     for(b=0; b<n; b++){
       if (key==*(a+b)){
           return TRUE;}
                                    /*search for values with pointer*/
     }
    return FALSE;
    }
    
    int main()
    {
    int k, j=9;
    int array[]={10, 20, 13, 32, 50, 25, 40, 49, 11, 12};
    
    k=10;
    if (search(array,j,k)==TRUE){
      printf ("%d does exist\n", k);}
    else if(search(array,j,k)==FALSE){
      printf ("%d does not exist\n", k);}
    
    k=145;
    if (search(array,j,k)==TRUE){
      printf ("%d does exist\n", k);}
    else if(search(array,j,k)==FALSE){
      printf ("%d does not exist\n", k);}
    
    k=15;
    if (search(array,j,k)==TRUE){
      printf ("%d does exist\n", k);}
    else if(search(array,j,k)==FALSE){
      printf ("%d does not exist\n", k);}
    
    k=11;
    if (search(array,j,k)==TRUE){
      printf ("%d does exist\n", k);}
    else if(search(array,j,k)==FALSE){
      printf ("%d does not exist\n", k);}                        /*run function with values*/
    
          system("PAUSE");
    }
    Last edited by pxleyes; 03-23-2004 at 05:10 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. two-dimensional dynamic array of pointers to classes
    By Timo002 in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 06:18 AM
  2. how do i declare array of pointers to base clase reobject
    By icantprogram in forum C++ Programming
    Replies: 3
    Last Post: 12-20-2002, 02:30 AM
  3. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  4. Loading an array using pointers
    By cheesehead in forum C++ Programming
    Replies: 5
    Last Post: 12-10-2001, 05:23 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM