-1

I'm new in c/c++ and confused because of char array (char[]) initializing
below is my code.

#include <iostream>
using namespace std;


struct Node {
    char key[5];
    int value;
    Node* next;
};

Node* tb[10]; // 해시 테이블(해당 인덱스에 리스트로 작성)
char* keys[5]; // 키는 중복 비교를 위해 따로 저장

void m_strcpy(char dsc[], char src[]) {
    while (*src != '\0')
        *(dsc++) = *(src++);
}



int main() {
    Node* newNode = new Node;
    char key[4] = "1";
    newNode->key = "1";

    return 0;
}

first

char key[4] = "1"; 

is ok. but below code gives me "expression must be a modifiable lvalue error".

newNode->key = "1";

well newNode->key is char key[5]. and is same as char key[4] i think. but why the difference happens?

thanks for reading my question.

  • `char key[4] = "1"; ` is initialization (creating a new array), while `newNode->key = "1";` is assignment. There are special rules allowing initialization with a string. But assigning a whole array is not possible. – BoP Aug 12 '22 at 07:10
  • Refer to [how to ask](https://stackoverflow.com/help/how-to-ask) where the first step is to *"search and then research"* and you'll find plenty of SO related posts for this. For example, this: [Expression must be Modifiable lvalue (char array)](https://stackoverflow.com/questions/37016819/expression-must-be-modifiable-lvalue-char-array) – Jason Aug 12 '22 at 07:10
  • 1
    Also, `C` and `C++` are different languages. Tag only for which you're looking an answer for. Additionally, refer to a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Jason Aug 12 '22 at 07:15

2 Answers2

0

You cannot assign to an array in this fashion. If you insist on using C-style char array strings, you must use one of the corresponding functions, like strcpy.

strcpy(newNode->key, "1");
char key[4] = "1";

This works because it is initialization of a char array, rather than assignment.

You would be well-advised to use std::string, though.

Please note also that there is no need to dynamically allocate newNode at all in this case, and you have not deleted it.

You could have avoided this by not dynamically allocating in the first place:

int main() {
    Node newNode;
    strcpy(newNode.key, "1"); 

    return 0;
}

Or by using a smart pointer, which handles deallocation when the pointer goes out of scope.

int main() {
    auto newNode = std::make_unique<Node>();
    strcpy(newNode->key, "1"); 

    return 0;
}
Chris
  • 26,361
  • 5
  • 21
  • 42
0

The first one is an initialisation, and the second is an assignment.

You can initialise an array but you cannot assign to one. Now you might reasonably wonder why this is, but the rules concerning arrays in C++ go back to the C language and it's rules. In C++ you should use std::string and std::vector instead. These have much more reasonable rules.

For example here's your code rewritten using std::string

#include <iostream>
#include <string>
using namespace std;


struct Node {
    string key;
    int value;
    Node* next;
};

Node* tb[10]; // 해시 테이블(해당 인덱스에 리스트로 작성)
string keys[5]; // 키는 중복 비교를 위해 따로 저장

int main() {
    Node* newNode = new Node;
    string key = "1";
    newNode->key = "1";  // with std::string this now compiles

    return 0;
}
john
  • 85,011
  • 4
  • 57
  • 81
  • I would suggest removing the extraneous variables `tb` and `keys` as well as the extraneous `key` inside `main`. – Chris Aug 12 '22 at 07:32