Solved

Array manipulation in function

Posted on 2004-09-29
10
220 Views
Last Modified: 2010-04-15
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
Comment
Question by:arut
10 Comments
 
LVL 12

Expert Comment

by:stefan73
ID: 12178174
Hi arut,
Yes, as the argument is passed by value, you can change:

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


Cheers!

Stefan
0
 
LVL 16

Accepted Solution

by:
PaulCaswell earned 25 total points
ID: 12178637
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
 
LVL 84

Expert Comment

by:ozo
ID: 12179059
func (char *p){
    char *q=(char *)malloc(strlen(p)+1);
    strcpy(q,p);
    strcpy(p,"green");
    strcpy(p,q);
}
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 4

Expert Comment

by:pankajtiwary
ID: 12184563
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
 

Author Comment

by:arut
ID: 12188744
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
 
LVL 7

Assisted Solution

by:aib_42
aib_42 earned 25 total points
ID: 12202370
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
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 12215224
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now