Solved

Newbie Question I can't get answered

Posted on 2002-05-16
3
173 Views
Last Modified: 2011-10-03
I have had this issue for years now, just never had the nerve to ask what the issue was..  I tired of it and would like to resolve this.

I tried this:
===================
char* Foo(char *pChar)
{
    sprintf(pChar, "My Test");
    return pChar;
}
void myFun()
{
    char pChar[50];
    Foo(&pChar);
    printf(pChar);
}
===================

Of course this doesn't work.  I get "cannot convert parameter 1 from 'char (*)[50]' to 'char *'"

This is what I have done to work around that problem and for years, I have just dealt with the warning of "return a tmp var".

char* Foo()
{
    char pChar[50];
    sprintf(pChar, "My Test");
    return pChar;
}
void myFun()
{
    char pChar[50];
    sprintf(pChar, Foo());
    printf(pChar);
}

What am I missing to make the first way work?
0
Comment
Question by:Chizl
3 Comments
 
LVL 4

Accepted Solution

by:
oumer earned 200 total points
ID: 7014682
instead of
   Foo(&pChar)

use
  Foo(pChar)

because the name of the array is the a pointer to that array

when you use &pChar you are taking the address of the array pointer, not that of array

pChar is also the same as &pChar[0], so this
 Foo(&pChar[0]) works


0
 
LVL 22

Expert Comment

by:ambience
ID: 7015446
char* Foo()
{
   static char pChar[50];
   sprintf(pChar, "My Test");
   return pChar;
}

this should get rid of the warning, note the warning that you have been ignoring was a fatal one, i reckon that as an error because the end result of ignoring that could be fatal.


And for the first part using a  

Foo(pChar);
instead of
Foo(&pChar);

could resolve the issue , also mentioned in the above post.


0
 
LVL 4

Author Comment

by:Chizl
ID: 7015454
I don't know why I felt I needed to pass in the reference, but I just did..  hehe  OK, thanx guys..
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now