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. 7 1220 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.
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. 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. 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
<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. 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 ---
Thanks to all, its run.
I will have to learn more and more C, xD. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Alex |
last post by:
Is there any problem with sending function pointers through in a
variable argument list? I have a function like the following:
typedef (*ptr2FuncType1)( int );
typedef (*ptr2FuncType2)( double...
|
by: Thomas Matthews |
last post by:
Hi,
I would like to create a table (or vector) of pointers
to templated functions.
1. How do I declare a typedef of a pointer to a templated
function?
For example, I have some functions...
|
by: Mikhail Teterin |
last post by:
Hello!
I'd like to have a variable of a pointer-to-function type. The two possible
values are of type (*)(FILE *) and (*)(void *). For example:
getter = straight ? fgetc : gzgetc;
nextchar...
|
by: munni |
last post by:
hi
i want to write a program on function pointers where i have not
written any programs using function pointers till now. i want to take
an array of 26 functions which r to be pointed by this...
|
by: cps |
last post by:
Hi,
I'm a C programmer taking my first steps into the world of C++.
I'm currently developing a C++ 3D graphics application using GLUT
(OpenGL Utility Toolkit written in C) for the GUI...
|
by: Christian Christmann |
last post by:
Hi,
in which situations pointers to functions might be more
efficient/convenient than a direct function call?
In the example I've found so far, I see no advantage of
using pointers to...
|
by: Bushido Hacks |
last post by:
A private utility function using a function pointer sounds ideal to
me. I want to simpify writing the same set of loops. While there are
a few functions that can't use it, setting and modifying...
|
by: claudiu |
last post by:
Hi,
I'll go straight to the first question. Why does the code below
compile?
void f(int i = 0);
int main()
{
(&f)();
|
by: bgold |
last post by:
Hey. I have a base class (SPRITE), and using this base class I have
derived a large number of derived classes (PERSON, BULLET, MISSILE,
etc.). Now, at a certain point in my program, I have a pair...
|
by: a |
last post by:
Hello.
Suppose I have a family of functions
f_0(x) = 0*x
f_1(x) = 1*x
....
f_n(x) = n*x
taking float and returning float (naturally, the actual functions would
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |