By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,986 Members | 2,030 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,986 IT Pros & Developers. It's quick & easy.

A question of pointers and functions.

P: n/a
Hi,

In this code, when i do lang = str, why,after, when it return to main
l don't contain hello?

Is not possible save a direction of str to lang and pass to main?

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

void file_config(char *str,char *lang)
{
puts(str);
lang = str;
}

int main()
{
char *homePtr = (char *) malloc(sizeof(char));
char *l;

homePtr="hello";
file_config(homePtr,l);
puts(l);
}

Thanks.

Jun 16 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
dr*******@gmail.com wrote:
Hi,

In this code, when i do lang = str, why,after, when it return to main
l don't contain hello?

Is not possible save a direction of str to lang and pass to main?

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

void file_config(char *str,char *lang)
{
puts(str);
lang = str;
Lang is passed by value, so all this line does is change the local copy,
not the value passed in. You want either

void file_config(char *str, char **lang)
{
puts(str);
*lang = str;
}

or

char* file_config(char *str)
{
puts(str);
return str;
}
--
Ian Collins.
Jun 16 '07 #2

P: n/a
In article <11**********************@q69g2000hsb.googlegroups .com>,
<dr*******@gmail.comwrote:
>In this code, when i do lang = str, why,after, when it return to main
l don't contain hello?
file_config(homePtr,l);
>void file_config(char *str,char *lang)
{
puts(str);
lang = str;
}
C passes arguments "by value". That is, the value of ; is copied to
lang when the function is called. Changes to lang don't affect l.

An alternative used in some other languages is call by reference. In
that case, lang would be made to refer to the same memory as l, so
changes to lang would change l. But C isn't like that.

You can achieve the same effect by passing the address of the variable.
Declare file_config to take a pointer to pointer to char:

void file_config(char *str, char **lang)

and make it dereference that pointer when it does the assignment:

*lang = str;

Then pass the address of l to the function:

file_config(homePtr, &l);

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Jun 16 '07 #3

P: n/a
dr*******@gmail.com wrote:
Hi,

In this code, when i do lang = str, why,after, when it return to main
l don't contain hello?

Is not possible save a direction of str to lang and pass to main?

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

void file_config(char *str,char *lang)
{
The first parameter should be const char*, you don't change its value.
puts(str);
lang = str;
}

int main()
{
char *homePtr = (char *) malloc(sizeof(char));
I forgot to point out that you don't (many will say shouldn't) cast the
return of malloc and sizeof(char) is by definition 1. So this line
should be

char *homePtr = malloc(1);
char *l;

homePtr="hello";
You don't want to do this either, at least not after the malloc. This
line changes the value of homePtr from whatever you got from malloc to
the address of the string literal "hello". This causes a memory leak,
the memory allocated with malloc is lost because you can never give it
back with free.

Just write

const char *homePtr="hello";
file_config(homePtr,l);
puts(l);
}

Thanks.

--
Ian Collins.
Jun 16 '07 #4

P: n/a
dr*******@gmail.com wrote, On 16/06/07 08:46:
Hi,

In this code, when i do lang = str, why,after, when it return to main
l don't contain hello?

Is not possible save a direction of str to lang and pass to main?
This is a FAQ. You can find the FAQ at http://c-faq.com and the specific
question is 4.8
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

void file_config(char *str,char *lang)
{
puts(str);
lang = str;
}

int main()
{
char *homePtr = (char *) malloc(sizeof(char));
You do not need to cast the value returned by malloc and it can hide a
serious error (which you have not made) of failing to include stdlib.h,
also sizeof(char) is 1 by definition. Also any time you are mallocing a
single char you should ask yourself whether you are using the right
approach.
char *l;

homePtr="hello";
This shows you need to read up on pointers a lot more than you thought.
It changes where homePtr is pointing, it does not put "hello" in to the
area you malloced earlier. If it did then you would have even bigger
problems since 4 characters plus a null termination do not fit in to one
character!
file_config(homePtr,l);
puts(l);
}
You should re-read the relevant sections of your text book and also all
the sections in the FAQ about pointers, memory allocation and arrays.
--
Flash Gordon
Jun 16 '07 #5

P: n/a

<dr*******@gmail.comha scritto nel messaggio
news:11**********************@q69g2000hsb.googlegr oups.com...
Hi,

In this code, when i do lang = str, why,after, when it return to main
l don't contain hello?

Is not possible save a direction of str to lang and pass to main?

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

void file_config(char *str,char *lang)
void file_config(const char *str, char **lang)
(see below)
{
puts(str);
lang = str;
*lang = str;
}

int main()
{
char *homePtr = (char *) malloc(sizeof(char));
char *homePtr = malloc(strlen("hello") + 1);
1 is for the terminating '\0'.
char *l;

homePtr="hello";
strcpy(homePtr, "hello");
What you wrote makes homePtr point to the string literal "hello"
itself, not copy the latter. If this is what you were trying to do,
why did you use malloc()? If you wanted to copy it instead, use
strcpy.
file_config(homePtr,l);
file_config(homePtr, &l);
Your call passes the *value* of l to the function, which of course
will be unable to change the value which l has in main().
You want to pass the *address* of l, and to change the type of that
parameter accordingly (as shown above).
puts(l);
free(homePtr);
(Any barely decent OS will do that for you, but better avoid getting bad
habits as soon as possible.)

return 0;
}

Thanks.
HTH.
Jun 16 '07 #6

P: n/a
dr*******@gmail.com wrote:
Hi,

In this code, when i do lang = str, why,after, when it return to main
l don't contain hello?

Is not possible save a direction of str to lang and pass to main?

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

void file_config(char *str,char *lang)
{
puts(str);
lang = str;
}

int main()
{
char *homePtr = (char *) malloc(sizeof(char));
char *l;

homePtr="hello";
file_config(homePtr,l);
puts(l);
}

Thanks.
Maybe this way..

#include<stdio.h>

void file_config(char *str, char **lang)
{
puts(str);
*lang = str;
}

int main(void)
{
char *l, *homePtr = "hello";
file_config(homePtr, &l);
puts(l);
return 0;
}

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Jun 16 '07 #7

P: n/a
Thanks to all, its run.

I will have to learn more and more C, xD.

Jun 16 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.