2

I am learning some data structure stuff and I faced a problem that I did not even expected because normally I can solve this kind of error.

#include <iostream>
using namespace std;

struct Avion {
    char* numePilot;
    int nrPasageri;
};

struct nodDublu {
    Avion info;
    nodDublu* next;
    nodDublu* prev;
};

struct ListaDubla {
    nodDublu* first;
    nodDublu* last;
};

Avion creareAvion(char* numePilot, int nrPasageri) {
    Avion avion;
    avion.numePilot = (char*)malloc(sizeof(char)*(strlen(numePilot) + 1));
    strcpy_s(avion.numePilot, strlen(numePilot) + 1, numePilot);
    avion.nrPasageri = nrPasageri;

    return avion;
}

nodDublu* creareNod(Avion info, nodDublu* next, nodDublu* prev) {
    nodDublu* nou = (nodDublu*)malloc(sizeof(nodDublu));
    nou->info = creareAvion(info.numePilot, info.nrPasageri);
    nou->next = next;
    nou->prev = prev;

    return nou;
}

ListaDubla inserareInceput(ListaDubla lista, Avion avion) {
    nodDublu* nou = creareNod(avion, lista.first, NULL);
    if (lista.first) {
        lista.first->prev = nou;
        lista.first = nou;
        return lista;
    }
    else {
        lista.first = nou;
        lista.last = nou;
        return lista;
    }
}

void main() {
    ListaDubla lista;
    lista.first = NULL;
    lista.last = NULL;

    Avion avion = creareAvion("Ionescu", 34);
}

I put here the entire code, but obv the problem is somewhere at "creareAvion" which is the funct for creating Avion I guess. That "Ionescu" is not working because of that error.

Andreea
  • 33
  • 2
  • 1
    Well, what happens if you try to dereference `numePilot` and modify it? How should the literal `"Ionescu"` be modified? – TrebledJ Jun 07 '19 at 19:47
  • You're using C++, so forget about `malloc` and use `std::string` instead of `char*`. Or if you really insist on using C in C++, change parameter `char* numePilot` to `const char* numePilot` in `creareAvion` – Yksisarvinen Jun 07 '19 at 19:47

1 Answers1

0

You are passing string literal "Ionescu" to a function that takes a char* as argument. In C++, in contrast to C, string literals will decay to pointers of type const char*, so this is simply a type mismatch.

To overcome this, I'd suggest to change your function to take a const char*, since you actually do not modify the contents of the passed parameter in the body:

Avion creareAvion(const char* numePilot, int nrPasageri) {
  ...

And: If you are using C++, switch to the metaphors of C++ (and get rid of those from C); C++ offers, for example, std::string, which avoids a lot of errors that can happen when coping with malloc/free ... in conjunction with copyable/moveable objects.

Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58
  • Technically, a string literal is not a pointer at all. It is an array. – eerorika Jun 07 '19 at 19:55
  • The thing is that this is not really my choice. I am a student and they want us to use those things. – Andreea Jun 07 '19 at 19:56
  • 1
    @Andreea do what you have to do to pass the class, but know that you are begin taught C rather than C++. This is unfortunately a common problem. If it continues, I recommend that you supplement your education with [a few good C++ references](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and some extra study when you have the opportunity so that you don't suffer so nasty a shock when you find yourself expected to actually know C++. At a job interview for example. – user4581301 Jun 07 '19 at 20:07