Return of a pointer...

T

Thomas Junior

Ok, I'm very confused.

I expected this would return a string, but intead the following function
returns a pointer:

char * CPUInfo::GetVendorID ()
{
// Return the vendor ID.
switch (ChipManufacturer) {
case Intel:
return "Intel Corporation";
case AMD:
return "Advanced Micro Devices";
case NSC:
return "National Semiconductor";
default:
return "Unknown Manufacturer";
}
}

I want the calling function to have access to that string. Without changing
the above function, how could I access that string using the pointer
returned? Everything I've tried hasn't worked.

Thanks!



--

Tom Junior
(e-mail address removed)

Automated Design Corporation
P: (630) 783-1150 F: (630) 783-1159
 
R

Ron Natalie

Thomas Junior said:
I want the calling function to have access to that string. Without changing
the above function, how could I access that string using the pointer
returned? Everything I've tried hasn't worked.
WHat do you mean it hasn't worked. All those strings inside the switch
are statically allocated. It should work, i.e.,

cout << CPUInfo::GetVendorID();

should work fine.

char* is a pointer NOT A STRING. If you want to return a string, write the function:

#incldue <string>
using std::string;

string CPUInfo::GetVendorID() {
// ... rest o the function requires no changes.
 
D

Default User

Thomas said:
Ok, I'm very confused.

I expected this would return a string, but intead the following function
returns a pointer:

char * CPUInfo::GetVendorID ()

Let's see, you defined a function that returns a pointer to char, then
are offended because it returns a pointer? I'm perplexed.
I want the calling function to have access to that string. Without changing
the above function, how could I access that string using the pointer
returned? Everything I've tried hasn't worked.


What do you mean "have access"? What things have you tried? Post a
complete, minimal program that demonstrates the problem.

It should be noted that C-style strings are tricky for the new
programmer. You'd be better off with the C++ string class. Your text
should discuss it, if not get a new text.



Brian Rodenborn
 
J

Jon Bell

I expected this would return a string, but intead the following function
returns a pointer:

Of course it returns a pointer. You defined it that way! :) char *
is a pointer to char.
char * CPUInfo::GetVendorID ()
{
// Return the vendor ID.
switch (ChipManufacturer) {
case Intel:
return "Intel Corporation";
case AMD:
return "Advanced Micro Devices";
case NSC:
return "National Semiconductor";
default:
return "Unknown Manufacturer";
}
}

I want the calling function to have access to that string. Without changing
the above function, how could I access that string using the pointer
returned? Everything I've tried hasn't worked.

If you want to return a strong, declare the function to return a string.
C++ has a standard string data type. Use it, unless you have a really
good reason to avoid it. It will make your life simpler than if you
continue to muck around with char pointers.

#include <string>

std::string CPUInfo::GetVendorID ()
{
switch (ChipManufacturer) {
case Intel:
return "Intel Corporation":
// etc.
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,786
Messages
2,569,625
Members
45,321
Latest member
AlphonsoPi

Latest Threads

Top