-1

Hello I'm still a beginner in c++ or coding in general.So my lecturer didn't teach us about pointer.

so i search the internet and you would need to use char* to call it.since I cannot use char* I only can use char name();

char* name();// example for char* I want without *
char*name(){
        char name[50];
        cout<<"name:";
        cin.getline(name,50);

        return name;
}

int main(){
      char staff[50];
      strcpy(staff,name());
      cout<<"name:"<<staff;

     return 0;
     }

How to return the name to the main function without using char* and use char(); (note that I could use reference but I don't know if char could use for it too). Thanks in advance

  • 11
    You should probably be using `std::string`. – George May 20 '19 at 14:04
  • 4
    Never name a variable the same as the function you are in. This is confusing to the reader. – drescherjm May 20 '19 at 14:07
  • 3
    `char*name(){` when the `name` function ends your local variable `name` no longer exists so the pointer that you return is not valid. Google for dangling pointer. – drescherjm May 20 '19 at 14:08
  • 2
    "*my project needs to use function without parameter*" Can you explain why this is? It's very hard to solve a problem when you don't understand what the actual problem is and only know someone's suggestion for how to solve it. – David Schwartz May 20 '19 at 14:09
  • `char*` is a pointer not an array. you cannot return c-arrays from functions – 463035818_is_not_an_ai May 20 '19 at 14:09
  • 2
    if you drop `char*` and use `std::string` your code is as simple as `std::cin >> name;`. Is it a requirement to write a function? I agree with DavidSchwartz, it is not clear what is part of your solution and what is a real requirement. It seems you already realized that using `char*` isnt the solution, are you allowed to use `std::string` ? – 463035818_is_not_an_ai May 20 '19 at 14:12
  • No I wasn't allowed to use std:: string – Muhd Alif Haiqal Hazizi May 20 '19 at 15:39
  • @david schwartz it is just the requirement for my project. I know you can return a single char without using pointer but when dealing with a string, it becomes messy.. – Muhd Alif Haiqal Hazizi May 20 '19 at 15:41
  • @MuhdAlifHaiqalHazizi What, precisely, is the requirement? Is it that the function not take any parameters? Not return any parameters? Or what exactly? – David Schwartz May 21 '19 at 00:21
  • It is function that takes no parameters and it return it own parameter to the main function. – Muhd Alif Haiqal Hazizi May 21 '19 at 00:46
  • Are you allowed to create a class that you can return? Otherwise, the assignment doesn't make sense to me. – lakeweb May 21 '19 at 00:56
  • what must the ``name`` function even do? Because with the information there is now I only can come up with the fact that this is really stupid – Tarick Welling May 21 '19 at 07:09

2 Answers2

3

The code you are looking for looks like this:

#include <iostream>
#include <string>

std::string name();

int main() {
  std::string staff;
  staff = name();
  std::cout << "name:" << staff << std::endl;

  return 0;
}

std::string name() {
  std::string aName;
  std::cout << "name: ";

  std::getline(std::cin, aName);
  return aName;
}

std::string is a class which manages the allocation, access and deallocation of the buffer wherein the character data is stored. Internally it has a pointer to the data. This object can easily be value copied and even moved if you want the pointed data to remain in place.

These move semantics would look like: staff = std::move(name()); This will only copy the pointer to the data and not the data pointed to by the pointer.

Individual pointers should never be used to transfer ownership in C++. unique_ptr and shared_ptr should be used.

If you really want to return the data using a raw pointer (which is a really bad idea) you will have to use new and new[] because this will allocate space on the free store which will remain usable when the function reaches the end of its scope. But it also makes the outer function responsible for the deallocation of the memory when the program ends with a call to delete or delete[].

Tarick Welling
  • 3,119
  • 3
  • 19
  • 44
  • 1
    If you must use 'new', never forget to 'delete' when you're done with it. Likewise, if you use 'new[]', you must use 'delete[]'. MUST. – Mark Storer May 20 '19 at 14:43
  • Yeah about the std:: string.. we can't use it.. can you show the really bad idea? Sorry to bother you. – Muhd Alif Haiqal Hazizi May 20 '19 at 15:44
  • 1
    @MuhdAlifHaiqalHazizi https://stackoverflow.com/questions/15179996/how-should-i-allocate-memory-for-c-string-char-array that could be a duplicate for this question but the person asking the question changed the question after the answer showed to use new. – drescherjm May 20 '19 at 16:21
  • The original code for that answer is here: https://stackoverflow.com/revisions/15179996/1 – drescherjm May 20 '19 at 17:27
0

Since you're not allowed to use std::string, and want to return by char * you may do something like this:

auto name() {
  char* p = new char[50]{};
  std::cin.getline(p, 50);
  return p;
}

int main() {
  auto n = name();
  std::printf("%s\n", n);
  delete[] n; // now you're responsible to free this
}
Andreas DM
  • 10,685
  • 6
  • 35
  • 62