0

I'm trying to set a pointer array to a char array in class Tran, however it only applies the first letter of the string. I've tried many other ways but can't get the whole string to go into name.

edit: name is a private variable

char name[MAX_NAME + 1];

Trying to output it using cout << name << endl;

the input is:

  setTran("Birth Tran", 1);

help would be appreciated, thank youu

6
  • Is there any reason why you want to use char arrays and not std::string? Commented Feb 1, 2020 at 4:24
  • @infinitezero maybe C compatibility? I dunno. Commented Feb 1, 2020 at 4:30
  • @infinitezero code i'm working with requires me to use char arrays Commented Feb 1, 2020 at 4:30
  • Anyways, what you want to do is to copy the pointer address to the pointer. Currently you dereference the pointer namee (which is the first character) and save that at the first index position of the array. Commented Feb 1, 2020 at 4:32
  • @infinitezero sorry, how exactly would I be able to do that? I've just tried a couple things but get errors because it's a const Commented Feb 1, 2020 at 4:35

2 Answers 2

1
namee[0] == NULL
name[0] = NULL;

These are bugs. NULL is for pointers. name[0] as well as namee[0] is a char. It may work (by work, I mean it will assign the first character to be the null terminator character) on some systems because 0 is both a null pointer constant and an integer literal and thus convertible to char, and NULL may be defined as 0. But NULL may also be defined as nullptr in which case the program will be ill-formed.

Use name[0] = '\0' instead.

name[0] = *namee;

however it only applies the first letter of the string.

Well, you assign only the first character, so this is to be expected.

If you would like to copy the entire string, you need to assign all of the characters. That can be implemented with a loop. There are standard functions for copying a string though; You can use std::strncpy.

That said, constant length arrays are usually problematic because it is rarely possible to correctly predict the maximum required size. std::string is a more robust alternative.

Sign up to request clarification or add additional context in comments.

Comments

0

The underlying issue you are trying to assign a const char* to an char* const. When declaring

char name[MAX_NAME + 1];

You are declaring a constant memory address containing mutable char data (char* const). When you are passing a const char* to your function, you are passing a mutable pointer containing constant data. This will not compile. You should be doing a deep copy of the char array by using:

strcpy_s(dst, buffer_size, src);

This copy function will make sure that your array does not overflow, and that it is null terminated.

In order to be able to assign a pointer to a char array, it would need to be allocated on the heap with

char* name = new char[MAX_NAME + 1];

This would allow assigning a char* or char* const to it afterwards. You however need to manage the memory dynamically at this point, and I would advise against this in your case, as passing "Birth Tran" would lead to undefined behaviours as soon as char* const namee goes out of scope.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.