472,958 Members | 2,397 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

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 2450

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Alex Vinokur | last post by:
Various forms of argument passing ================================= C/C++ Performance Tests ======================= Using C/C++ Program Perfometer...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
3
by: Steven Taylor | last post by:
Hope someone can assist. I'm trying to answer a book question. I'm going around in circles in relation to 'pointer-to-char'. Object : A short program is to be created, which involves a...
10
by: Pete | last post by:
Can someone please help, I'm trying to pass an array to a function, do some operation on that array, then return it for further use. The errors I am getting for the following code are, differences...
3
by: ZMan | last post by:
The following code won't compile with gcc version 3.4.2 (mingw-special). How come? Error: cannot convert `char (*)' to `char**' /**********************************************************/...
3
by: Amit_Basnak | last post by:
Dear friends I have to pass the objec of a class which is a part of afunction in thefunction call. my code looks like this now #include <iostream.h> #include <waspc/common.h> #include...
11
by: Bob Yang | last post by:
Hi, I have this in C++ and I like to call it from c# to get the value but I fail. it will be good if you can give me some information. I tried it in VB.net it works but I use almost the same way as...
6
by: Andy Baker | last post by:
I am attempting to write a .NET wrapper for a C++ DLL file, but am having problems with passing strings as parameters. How should I be writing my C# function call when the C header file is...
13
by: Andy Baker | last post by:
I am attempting to write a .NET wrapper in C# for an SDK that has been supplied as a .LIB file and a .h header file. I have got most of the functions to work but am really struggling with the...
4
by: arnuld | last post by:
I am passing an array of struct to a function to print its value. First I am getting Segfaults and weired values. 2nd, is there any elegant way to do this ? /* Learning how to use an array...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.