Doubt

V

vhanwaribrahim

Hi all,

please see the below code.

int main()
{
char *a;
a= malloc(20);
strcpy("a,"India"");
change(a);
printf("%s\n",a);
}

void change (char *b)
{
b="Testing";

}

The o/p of this program is India. can any one tell how this works.






}
 
C

Chris Dollin

Hi all,

please see the below code.

int main()
{
char *a;
a= malloc(20);
strcpy("a,"India"");

I don't think so. I think that line was supposed to be:

strcpy( a, "India" );

As it happens, those three lines could have been replaced
by

char *a = "India";

since you're not writing into any of its characters.
change(a);
printf("%s\n",a);

You forgot to #include said:

Also in this case it probably doesn't matter, you've got a
store leak: the mallocated store you point to with `a` isn't
freed. Since the program will now exit, and since many
hosted environments will reclaim all program store on
exit, you're probably OK; but it's a good idea to start
learning store management /now/.
void change (char *b)
{
b="Testing";

This /does not change main's `a`/. It is just an assignment
of a pointer value to the local variable `b` ...

.... which evaporates when `change` returns.

C does not have pass-by-reference, and this is an example that
illustrates it. The parameter `b` isn't a reference to the
actual argument `a`; it just has a copy of `a`s value.
The o/p of this program is India. can any one tell how this works.

Error: unexpected brace. (Braces come in pairs, hence [1] the name.)

[1] For some value of `hence`.
 
S

santosh

Hi all,

please see the below code.

int main()

int main(void) is better.
{
char *a;
a= malloc(20);
strcpy("a,"India"");

This is a syntax error. You probably meant to write:

strcpy(a, "India");
change(a);
printf("%s\n",a);

A return statement is better practise.
}

void change (char *b)
{
b="Testing";

}

The o/p of this program is India. can any one tell how this works.

This is because of the pass by value semantics of C. In C function
arguments are actually copied before the function is called. For
example:

void foo(void) {
int b = 10;
void bar(int);

bar(b);
printf("%d\n", b);
return;
}

void bar(int b) {
b = 20;
return;
}

Here a temporary copy of the object 'b' in foo() is created when the
function call to bar() takes place. The 'b' in bar() is local to that
function and any changes to it does not affect foo()'s copy of 'b'.
This is the case for all function arguments in C, except the case of
arrays. Here the array name decays to a pointer value to the array's
first element.

To enable a function to change it's caller's objects, you need to pass
pointers to them. This is automatically done in the case of arrays,
unless they are wrapped into structures.
 
M

Martin Ambuhl

int main()
{
char *a;
a= malloc(20);
strcpy("a,"India"");
change(a);
printf("%s\n",a);
}
void change (char *b)
{
b="Testing";
}
The o/p of this program is India. can any one tell how this works.

It doesn't work. The output, if any, is completely random, since your
code has gross errors. See below:
#include <stdio.h> /* mha: added. Needed for the variadic
function printf */
#include <stdlib.h> /* mha: added. Needed because malloc
returns a pointer-to-void, not an
int as your code implies (in C89,
things are worse under C99). */
#include <string.h> /* mha: added, for strcpy, which
returns a pointer-to-char, not an
int as your code implies. */

void change(char *); /* mha: needed, since your main expects
change to return an int. */

void new_change(char *); /* mha: a new version of change */

int main(void)
{
char *a;
if (!(a = malloc(20))) {
fprintf(stderr, "Malloc failed. Quiting ...\n");
exit(EXIT_FAILURE);
}

strcpy(a, " India "); /* mha: fixed the grossly incorrect
original 'strcpy("a," India "");' */
printf("contents of array pointed to by a: \"%s\"\n", a);
change(a);
printf("OP's change produces no change in a: \"%s\"\n", a);
new_change(a);
printf("mha's new_change does change a: \"%s\"\n", a);
free(a); /* mha: added. failure to free what you
allocate is a sin. */
return 0; /* mha: added for C89, and for sanity
since a function returning an int
should return one */
}

void change(char *b)
{
b = "Testing"; /* mha: changes the local value of the
pointer b, a copy of a in main */

}


void new_change(char *b)
{
strcpy(b, "Testing"); /* mha: changes the array whose first
element is pointed to by b */
}

[output]
contents of array pointed to by a: " India "
OP's change produces no change in a: " India "
mha's new_change does change a: "Testing"
 
R

Richard Heathfield

(e-mail address removed) said:
Hi all,

please see the below code.

int main()
{
char *a;
a= malloc(20);

You'll need <stdlib.h> for this.

Don't proceed further without checking that the allocation succeeded:
malloc returns NULL on failure.
strcpy("a,"India"");

You'll need <string.h> for this. Also, it should be:

strcpy(a, "India");
change(a);

You'll need to provide the prototype:

void change(char *b);

before calling this.
printf("%s\n",a);

You'll need <stdio.h> for this.

Since you no longer require the storage after this point:

free(a);

Don't forget to return a value from main - either 0 or EXIT_SUCCESS will do
fine.
}

void change (char *b)
{
b="Testing";

}

The o/p of this program is India. can any one tell how this works.

You pass a pointer value to change(). This is copied into an object that
exists only while the change() function is executing. The value of that
copy is changed by the change() function, but the original remains
unmodified. Hence the behaviour you have encountered. If you wish to
change the string itself, don't change the pointer value. Instead, remove
your b="Testing" and replace it with strcpy(b, "Testing") - this will have
the effect you seek.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,127
Latest member
CyberDefense
Top