-2

I'm at college and we're learning pointers. Our job was to input a char, compare it to an array and return a pointer to the first reference of that char in the array. But, as I don't like easy things, I've asked my teacher what about having that char more than once in the array. That's where my headache begins. So I have this code. The idea is: create a function that compares the input char to the entire array, get the pointers of the references and save them in an array and return that array. Unfortunately it's not working as I wish :( What can be wrong?

#include<iostream>
#include<cstdlib>
using namespace std;
char list [10];
int main()
{
    initialize();
    show();
    cout<<search('1');
}
void initialize()
{
    int i;
    for(i=0; i<10;i++)
    {
        list[i]='1';
    }
}
void show()
{
    int i;
    for(i=0; i<10;i++)
    {
        cout<<list[i];
    }
}
int* search(char* input)
{
    int* result[10];
    int i;
    char *temp;
    for (i=0; i<10; i++)
    {
        *temp=list[i];
        if(strcmp(temp, input) != NULL)
        {
            result[i]=i;
        }
    }
    return result[];
}
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
  • are you comparing the first 10 characters from the two strings? – Iharob Al Asimi Feb 22 '15 at 13:09
  • int *result[10] is an array of integer pointers. 'i' is an integer. result[i] = i; is assigning an integer to an integer pointer. This compiles because of C's slipperyness, but is probably not what you want. If you compiler has a strict option, use it and learn from it. – Randy Kamradt Sr. Feb 22 '15 at 13:13

1 Answers1

0

I'm on a mobile device so I can't go into huge detail unfortunately, but you are returning a pointer to an array that you create in the function which goes out of scope at the end of the function.

My massive edit:

As everyone has already stated, a C++ array is actually only a pointer to the first element in the array. As a result, if you return a pointer to an array created in the scope of the function, you are returning a pointer to garbage. If I were doing this I would use a vector, but if I were to be forced into using an array, I would use something like the code below. Hope this helps!

#include <iostream>
#include <cstdlib>

void initialize(char* list) {
    for(int i = 0; i < 10; ++i) {
        if(i < 4) {
            list[i] = '2';
        } else {
            list[i] = '1';
        }
    }
}

void show(char *list) {
    for(int i = 0; i < 10; ++i) {
        std::cout << list[i] << ' ';
    }
    std::cout << std::endl;
}

// Note the function requires an additional argument that is a pointer
// this is how you can avoid returning a pointer
int search(char input, char* list, char* result) {
    int j = 0;
    for(int i = 0; i < 10; ++i) {

        // comparing characters can be done via ==

        if(input == list[i]) {

            *(result + j) = list[i];

            // You could use result[j], but I used this to show that
            // result really is just a pointer to the first element
            // of the array. As a result saying result[j] is the same
            // as saying I want to deference j elements past the first
            // element or *(result + j)

            ++j; // increment j
        }
    }
    // return how many elements matched
    return(j);
}

int main(int argc, char *argv[]) {
    char list[10];
    char temp[10];

    initialize(list);
    show(list);
    int size = search('1', list, temp);

    // create a dynamically sized array containing space for each match
    // because we don't know the size at compile time we must use
    // a library type or a dynamically sized array
    char *result = new char[size];

    for(int i = 0; i < size; ++i) {
        result[i] = temp[i];
        // again you could use result[i]
        std::cout << *(result + i) << std::endl;
    }
    delete[] result; // otherwise you'll get a memory leak
    return(0);
}
Daniel Robertson
  • 1,354
  • 13
  • 22