Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Array manipulation in function

Posted on 2004-09-29
10
245 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
Easy, flexible multimedia distribution & control

Coming soon!  Ideal for large-scale A/V applications, ATEN's VM3200 Modular Matrix Switch is an all-in-one solution that simplifies video wall integration. Easily customize display layouts to see what you want, how you want it in 4k.

 
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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
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.

861 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