Solved

Newbie Question I can't get answered

Posted on 2002-05-16
3
172 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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 viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

772 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

9 Experts available now in Live!

Get 1:1 Help Now