pointer error

Pages: 12
1
2
3
4
5
6
7
8
9
10
11
12
bool FindElement(int array[],int *ptr,int size);
bool FindElement (int array[],int *ptr,int size)
{
    if (size==0)
    return false;
    else
    if (array[size]==*ptr)
    return true;
    else
    return FindElement(array,ptr++,size);

}


it give me error that initializing argument 2 of bool (int*,int*,int)
what is that mean?
i heard that if we didnt use the pointer is much eaiser but idk
oh sorry the error was in line 31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include "List.h"


using namespace std;


bool FindElement(int array[],int *ptr,int size);
bool FindElement (int array[],int *ptr,int size)
{
    if (size==0)
    return false;
    else
    if (array[size]==*ptr)
    return true;
    else
    return FindElement(array,ptr++,size);

}



int main()
{
    cout << "Hello world!" << endl;
    int array[10];
    int size=10;
    int *ptr;
    for(int i=0;i<10;i++)
        array[i]=i+1;
    FindElement (array,*ptr,size );

    IntSLList L1;
    for(int i=0;i<10;i++)
        L1.addToHead(i);
    cout<<L1.length();


    return 0;
}


here in the main FindElement (array,*ptr,size );
any idea?
ptr hasn't been initialised.
You're trying to use ptr (by dereferencing it) before you've initialised it.
i tried so many things i but int* ptr=ptr
i tried to remove the star but non one of them are working
What does int* ptr=ptr even mean? You're initialising ptr to... itself? What are you trying to do?

What is ptr supposed to be? A pointer to one of the elements in the array?
int* ptr = &something;

Here's one way to initialize it. When you pass ptr to the function, you're doing this:

FindElement (array,*ptr,size );
When you use * now, you're derferencing the pointer. So now your sending the value at the address that ptr holds rather than the pointer itself. So, initialize the pointer and then drop the asterisk in the function call.
i want write recursive function that returns true if certain key exists in an
array, false otherwise.
So what is ptr supposed to be pointing to?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include "List.h"


using namespace std;


bool FindElement(int array[],int element,int size);
bool FindElement (int array[],int element,int size)
{
    if (element==array[size])
    return true;
    else if (size==0)
    return false;
    else
    return FindElement(array,element,size++);
    }

int main()
{
    cout << "Hello world!" << endl;
    int array[10];
    int size=10;
    int element;
    for(int i=0;i<10;i++)
        array[i]=i+1;
    FindElement (array,element,size );

    IntSLList L1;
    for(int i=0;i<10;i++)
        L1.addToHead(i);
   // cout<<L1.length();


    return 0;


even when i did that i got the same error! for the element in the main!
if int* ptr=ptr have no meaning then how can i know the element which is in the array is there?
Sorry, I don't understand your question. I can see you've changed from using an int* to an int argument, but you're still comparing the value of array[i] to element without ever having given element a value in the first place.

What is it you want to do with element? What is it supposed to be set to?
Ok what's the point of ptr? You do realize that a pointer has to actually point at something to serve a purpose, right?
yea.. i used the pointer from the beginning to let it check whether the value is in the array if not ptr++ move to the next element in the array and so on that what i understand from the question let me post the question

Write a recursive function that returns true if certain key exists in an
array, false otherwise.
Hints:
• You may assume a fixed size for the array.
• array can be filled with any data you like, and the key to search is
upon your choice.
You don't even need a pointer for this then. You can just pass the array. But if you insist, then all you need to do is:

int* ptr = arr;

And now your pointer will point to the first element of the array, and using pointer arithmetic you can traverse the array. This is pretty unsafe though, much better just to use the array by itself, or even better would be a vector.
int* ptr = arr; i tried that before but its not working too ok if i wont use the pointer anymore then how i will find the element!
if i put size++ in order to move to the next element that will change the size itself! well the array size wont be able to change once we initialize it..
or array[10]++! it wont work
So... is element supposed to contain the value that you're trying to find in the array?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void doWorkOnArr(int arr[], size);
int main()
{
   int arr[10] = { /** Random stuff **/ };
   int size = 10;
   doWorkOnArr(arr, size);

   return 0;
}

void doWorkOnArr(int arr[], size)
{
   for(int i = 0; i < size; i++)
      {
         arr[i] = rand();
      }
}


Don't ever say I didn't do anything for you.
oh thanks thats kind of u.. dont worry i didnt copy and paste it cuz i have to write it recursively but thats really help me thanku so much
Well what I posted wasn't meant to be copy and pasted. Just an example on how to pass an array and then iterate through it.
Pages: 12