Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Array manipulation in function

Posted on 2004-09-29
10
Medium Priority
?
265 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
[X]
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
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 100 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 100 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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 reading files 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.

609 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