Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Newbie Question I can't get answered

Posted on 2002-05-16
3
Medium Priority
?
177 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

730 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