Solved

Newbie Question I can't get answered

Posted on 2002-05-16
3
174 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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…

830 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