Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Newbie Question I can't get answered

Posted on 2002-05-16
3
Medium Priority
?
178 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 800 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

971 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