473,465 Members | 1,405 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Return of a pointer...

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
to********@automateddesign.com

Automated Design Corporation
P: (630) 783-1150 F: (630) 783-1159
Jul 19 '05 #1
4 8512

"Thomas Junior" <to********@automateddesign.com> wrote in message news:CR*******************@newsread2.news.atl.eart hlink.net...
\

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.
Jul 19 '05 #2
Stupid, stupid, stupid...

Thanks. Works great, and I feel like an idiot.
"Ron Natalie" <ro*@sensor.com> wrote in message
news:3f*********************@news.newshosting.com. ..

"Thomas Junior" <to********@automateddesign.com> wrote in message news:CR*******************@newsread2.news.atl.eart hlink.net... \

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.

Jul 19 '05 #3
Thomas Junior wrote:

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
Jul 19 '05 #4
In article <CR*******************@newsread2.news.atl.earthlin k.net>,
Thomas Junior <to********@automateddesign.com> wrote:

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.
}

--
Jon Bell <jt*******@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Gama Franco | last post by:
Hi, I'm designing an interface for a shared library, and I would like to know if there is a standard about how to return an object to the user. I will use exceptions to report errors, so there...
5
by: Neal Coombes | last post by:
Posted to comp.lang.c++.moderated with little response. Hoping for better from the unmoderated groups: -------- Original Message -------- Subject: Return appropriately by value, (smart)...
14
by: William Ahern | last post by:
is this legal: FILE *func(void) { extern int errno; return (errno = EINVAL, NULL); } my compiler (gcc 2.9.x on openbsd) is complaining that i'm returning a pointer from int w/o a cast....
10
by: LaEisem | last post by:
On-the-job, I have "inherited" a lot of old C language software. A question or two about when "casting" of null pointer constants is needed has occurred during behind-the-scenes cleanup of some...
4
by: Isaac | last post by:
Hi mates I want to know a simple program of return array from function ? Do I need to use pointer to return the address of the first element in an array. Isaac
23
by: Nascimento | last post by:
Hello, How to I do to return a string as a result of a function. I wrote the following function: char prt_tralha(int num) { int i; char tralha;
4
by: msolem | last post by:
I have some code where there are a set of functions that return pointers to each other. I'm having a bit of a hard time figuring out the correct type to use to do that. The code below works but...
32
by: zl2k | last post by:
hi, c++ user Suppose I constructed a large array and put it in the std::vector in a function and now I want to return it back to where the function is called. I can do like this: ...
18
by: Pedro Pinto | last post by:
Hi there once more........ Instead of showing all the code my problem is simple. I've tried to create this function: char temp(char *string){ alterString(string); return string;
9
by: Francois Grieu | last post by:
When running the following code under MinGW, I get realloc(p,0) returned NULL Is that a non-conformance? TIA, Francois Grieu #include <stdio.h> #include <stdlib.h>
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.