473,387 Members | 1,532 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

How this peice of code will execute?

72
Hi All,

What is happening in the below piece of code
Its printing Hello, why not world. Its working like call by value. Please clarify me on this.

int main()
{
char *temp = (char *)malloc(sizeof("Hello" + 1));
strcpy(temp, "Hello");
fun(temp);
printf("temp = %s\n", temp);
return 0;
}
void fun(char *temp)
{
temp = (char *)malloc(sizeof("World" + 1));
strcpy(temp, "World");
}
Dec 16 '09 #1
4 1386
solita
17
I think you should use the following format.

func(&temp)

and

void fun(char **temp)


int main()
{
char *temp = (char *)malloc(sizeof("Hello" + 1));
strcpy(temp, "Hello");
fun(&temp);
printf("temp = %s\n", temp);
return 0;
}
void fun(char **temp)
{
*temp = (char *)malloc(sizeof("World" + 1));
strcpy(*temp, "World");
}

I am new to programming so I may be wrong.
Dec 16 '09 #2
Banfa
9,065 Expert Mod 8TB
manjuks, it is working as call by value because it is call by value. C has no call by reference. C++ does but that is not the syntax, that is call by value passing a pointer to char.

Your function, fun, takes a pointer. When it is called the value of a pointer to passed to the function. The code immediately overwrites this value with one of its own storing it in the local parameter temp and then copies data to that new location. The function then exits leaking the memory allocated because it is never freed. Back in main none of the variable values have changed value because C calls by value. The data pointed to by temp hasn't changed because the function, fun, never used the pointer that was passed to it, it overwrote it.

Like I said C only supports call by value. It does support a pseudo call by reference by passing a pointer by value. The called function can then use the pointer to access the calling codes copy of what was pointed to. For an arbitrary type T then

void fun(T *param);

is a function that uses call by value to pass the value of a pointer T * in param. The function can then use param to achieve a pseudo call by reference to a type T in the calling code.

T value = <TInitialiser>;

fun(&value);

The address of value is passed in call by value to the fun but fun can use that pointer to achieve a pseudo call by reference to value.

You wanted to call by reference a variable of type char *. Looking at my example that means that T has type char *, the function has a parameter type type T * so that would be char **.

To get pseudo call by reference to a variable of type char * the function needs to take a parameter of type char **. That is exactly what solitas example shows.
Dec 16 '09 #3
donbock
2,426 Expert 2GB
By the way, your program calls malloc() twice but never calls free(). This doesn't cause a problem for such a simple program, but it is a bad habit to get into. You should develop the discipline to always free dynamically allocated memory once you're finished with it.
Dec 16 '09 #4
manjuks
72
Hi,

Thanks a lot for your suggestions. I understood this properly.

Thanks,
Manjunath
Dec 17 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Rony | last post by:
A question on python source documentation. Does there exist a standard for documenting code ? I've included here an example generated by pydoc of one of my modules. Is this the right way or is it...
2
by: J Dubal | last post by:
Hello good people, Following works in FC1 (python-2.2.3-7, postgresql-7.3.4-11, kernel-2.4.22-1.2194.nptl, pyPgSQL-2.4) from pyPgSQL import PgSQL conn =...
7
by: Rick Caborn | last post by:
Does anyone know of a way to execute sql code from a dynamically built text field? Before beginning, let me state that I know this db architecture is built solely for frustration and I hope to...
25
by: Delta | last post by:
Drop Down Menu Mozilla : work well widowed elements such as drop downs, except for flash movies IE : work well so far http://pwp.netcabo.pt/falmartins/index.htm
88
by: Peter Olcott | last post by:
Cab you write code directly in the Common Intermediate language? I need to optimize a critical real-time function.
58
by: manoj1978 | last post by:
Hi All, I wrote the following to print an integer in its string representation for base -36 to 36. Please comment on this code. #include <iostream> #include <string> using std::abs; using...
3
by: Matthew Warren | last post by:
I have the following piece of code, taken from a bigger module, that even as I was writing I _knew_ there were better ways of doing it, using a parser or somesuch at least, but learning how wasn't...
28
by: Joey Martin | last post by:
One of my servers got hacked with the SQL injection due to poor coding. So, I had someone write a stored procedure and new code. But, to me, it looks just as flawed, even using the stored...
7
by: Service4PC | last post by:
Hi all, I've a problem... I'm writing an extension for php, and i need to execute a file php... but only the content of the file and not the file... Now I better explain: - the extension get the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
0
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...
0
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
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 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.