470,810 Members | 1,172 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,810 developers. It's quick & easy.

Passing char*

Hi everybody!

I would like to know what Im doing wrong here:

void name_function(char *filename) {
....
}

name_function("file.txt");

When I try to execute this, it doesn't work. "filename" is null, and I
dont know why.

Any suggestions?

Thank you very much!!!

Feb 9 '06 #1
6 2309

Perro Flaco wrote:
Hi everybody!

I would like to know what Im doing wrong here:

void name_function(char *filename) {
...
}

name_function("file.txt");

When I try to execute this, it doesn't work. "filename" is null, and I
dont know why.

Any suggestions?

Thank you very much!!!


Nothing seems wrong with the code snippets you've posted. When I run
the program below, I see

file.txt

printed as I would expect.

#include <iostream>

void name_function(char *filename)
{
std::cout << filename << "\n";
}

int main()
{
name_function("file.txt");
}

Can you post a minimal, complete program that demonstrates your
problem, following the guidelines in
http://www.parashift.com/c++-faq-lit...t.html#faq-5.8, and
people will be able to help.

Gavin Deane

Feb 9 '06 #2
On Thu, 09 Feb 2006 03:27:15 -0800, Perro Flaco wrote:
void name_function(char *filename) {
...
}
}
name_function("file.txt");

You are passing a constant and not a pointer. Try: void
name_function(const char *filename)

--
Hilsen/Regards
Michael Rasmussen
http://keyserver.veridis.com:11371/p...rch=0xE3E80917

Feb 9 '06 #3

Gavin Deane wrote:
#include <iostream>

void name_function(char *filename)
Should be

void name_function(const char *filename)

to avoid the deprecated conversion from string literal to non-const
char*, but I don't think that is the OP's problem.
{
std::cout << filename << "\n";
}

int main()
{
name_function("file.txt");
}


Gavin Deane

Feb 9 '06 #4

Michael Rasmussen wrote:
On Thu, 09 Feb 2006 03:27:15 -0800, Perro Flaco wrote:
void name_function(char *filename) {
...
}
}
name_function("file.txt");

You are passing a constant and not a pointer. Try: void
name_function(const char *filename)


I don't think that, in itself, that is the OP's problem. The conversion
from string literal to non-const char* is deprecated and should be
avoided, but it's not technically incorrect.

Of course, if the data pointed to by filename is modified in any way
inside name_function, that's undefined behaviour and all bets are off.
That could be the OP's problem, but without seeing the code, who knows
....

Gavin Deane

Feb 9 '06 #5
Perro Flaco wrote:
Hi everybody!

I would like to know what Im doing wrong here:

void name_function(char *filename) {
...
}

name_function("file.txt");

When I try to execute this, it doesn't work. "filename" is null, and I
dont know why.

Any suggestions?


Yes. Post a complete, minimal program that demonstrates the problem.

Brian
--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
Feb 9 '06 #6
Thank you very much for your interest and your help. Thanks!!! And
please, forgive me for replaying so late. I've been really busy at
work.

Well, I guess I found the problem. I was calling "name_function", after
doing some "strcat" operation with the "file.txt" char, and I think I
was doing something wrong with this function (strcat), because now
everything is working withouy any problem.

Again, thanks for your help!!!

Feb 13 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Steven Taylor | last post: by
11 posts views Thread by Bob Yang | last post: by
6 posts views Thread by Andy Baker | last post: by
13 posts views Thread by Andy Baker | last post: by
4 posts views Thread by arnuld | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.