• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 266
  • Last Modified:

Array manipulation in function

Please consider the foll. code:


char a[10] = "blue";

func(a);
***************************
func (char *p)
{

}

Is it possible to modify the array's contents to 'green' using the pointer but leave the original array unchanged when function returns.

Thanks,
asm
0
arut
Asked:
arut
2 Solutions
 
stefan73Commented:
Hi arut,
Yes, as the argument is passed by value, you can change:

func (char *p)
{
   p = "green";
}


Cheers!

Stefan
0
 
PaulCaswellCommented:
arut,

This will NOT change 'a', it will only change p.

func (char *p)
{
   p = "green";
}

This WILL change 'a'.

func (char *p)
{
   strcpy(p,"green");
}

Paul
0
 
ozoCommented:
func (char *p){
    char *q=(char *)malloc(strlen(p)+1);
    strcpy(q,p);
    strcpy(p,"green");
    strcpy(p,q);
}
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
pankajtiwaryCommented:
Why are we going so far and mallocing? I believe the following will do it.

void func(char* p) {
    strcpy(p, "green");
    return ;
}

Well I did not quite understand you with "modify the array's contents to 'green' using the pointer but leave the original array unchanged".

Arrays in C are always passed to functions by reference as opposed to other variables which are passed by value. So, if you make any changes to the contents of p, it will be reflected in the original array. Anyway, you can not change p by any means, I mean the function signature should be something like void func(char* const p). Experts please correct.
0
 
arutAuthor Commented:
Hi Pankaj,

I agree with you. This is what I answered someone in an interview, but the guy wasn't convinced. He thinks its possible to change 'blue' to 'green' but keep the original array unchanged. I thought it was a stupid question to ask, but posted this just incase I missed something!
0
 
aib_42Commented:
They must be thinking of another language where you can choose to pass some variables by value or by reference. Here is a slightly more technical explanation to disprove them:

As array names evaluate to the address of their first element, they are of type "pointer to element". When you pass 'a' to your function, you are passing a pointer, which means it will have a local copy of the pointer 'p', but direct access to the element its pointing to.

You can of course have workarounds like manually creating a copy or changing the contents back to their original values, but the language does not create a local copy itself.

Now C++ has some fancy stuff like references of which I know nothing about, so if the question was about C++ and not C, he might have been correct.
0
 
PaulCaswellCommented:
arut,

func (char *p)
{
   p = "green";
   // Any code from here on referencing 'p' will see it as "green". Perhaps this was what they were looking for.
}

Paul
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now