Solved

Switching from char tables to pointers

Posted on 2008-06-22
15
278 Views
Last Modified: 2010-04-01
Please see the code snippet.

I am practicing pointers, but I am a bit stuck on this exercise. I need to convert this code and use pointers to the maximum instead of char arrays. Ideally there should be no reference to the arrays at all, just pointers.
char *mon_strcpy(char destination[], char source[]) { 
  int index = 0; 
  while (source[index] != '\0') { 
    destination[index] = source[index]; 
    index++; 
  } 
  destination[index] = '\0'; 
  return destination; 
}
 
char *mon_strcpy(char destination*, char source*);

Open in new window

0
Comment
Question by:codeQuantum
  • 4
  • 4
  • 3
  • +2
15 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 150 total points
ID: 21843108
you can change all
source[index] to *(source+index)
0
 
LVL 5

Author Comment

by:codeQuantum
ID: 21843160
My question might seem basic, but I am new to C++ :

char source*

What does it mean when the star is after the variable name? ie var* instead of *var seen on pointers...

Also, is it possible to replace the array names inside the function parameters with pointers too? I mean on the following line :

char *mon_strcpy(char destination[], char source[]) {
0
 
LVL 84

Expert Comment

by:ozo
ID: 21843168
char *mon_strcpy(char *destination[], char *source) {
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 13

Assisted Solution

by:josgood
josgood earned 50 total points
ID: 21843180
In
   char *mon_strcpy(char destination[], char source[]) {
two arrays of char are being passed.

In
   char *mon_strcpy(char *destination[], char *source) {
an array of char pointers (char *destination[]) is being passed, along with an array of char
0
 
LVL 84

Expert Comment

by:ozo
ID: 21843185
sorry, [] should have been deleted
0
 
LVL 13

Expert Comment

by:josgood
ID: 21843188
>>is it possible to replace the array names inside the function parameters with pointers
Yes.  Pointers and arrays are two syntactically differ ways of talking about pretty much the same thing.

You can say
   char *mon_strcpy(char destination[], char source[]) {
      char *d = destination, *s = source;
and then operate on the 's' and 'd' pointers.
0
 
LVL 5

Author Comment

by:codeQuantum
ID: 21843196
Thanks

But why is the star * (indirect modifier) placed after the variable names (destination, and source) in the function call :

char *mon_strcpy(char destination*, char source*);
0
 
LVL 84

Expert Comment

by:ozo
ID: 21843227
the way it is being used, it looks like an error to me
0
 
LVL 11

Expert Comment

by:spoxox
ID: 21843242
As an alternative to mentioning an index (an array construct) during the copy operation, you can increment source and destination pointers as you copy each character:

while (*from != '\0')
  *to++ = *from++;

If this is unclear:
the "to" and "from" pointers are just addresses of stuff in memory. Incrementing the pointer (++) means changing the address to the address of the next item. This operation is similar to updating an array index to reference the next array element.

Remember: "to" is the pointer (an address); "*to" is the char it's pointing to. That's why the assignment is done on "*to" (update the char) rather than on "to" (change the address the pointer holds).

Forgive me if this is old news.



char *mon_strcpy(char destination*, char source*);
Does this compile?
0
 
LVL 17

Assisted Solution

by:rstaveley
rstaveley earned 200 total points
ID: 21844250
> char *mon_strcpy(char destination*, char source*);

That shouldn't compile. You could be muddling it up with being able to put the star on either side of the white space, which is OK.

i.e. The following are equivalent:

char *mon_strcpy(char* destination, char* source);
char* mon_strcpy(char *destination, char *source);
0
 
LVL 5

Author Comment

by:codeQuantum
ID: 21846053
I am doing exercises from an old C++ class I took years ago. It is written like that on the copy, but it is not impossible that it is a typo. In all cases it does not compile, so I guess it is an error.

>>  char *mon_strcpy(char *destination, char *source) {

If I point to the tables this way, will the last line on my code work?

I am speaking of the following line :

>> return destination;

1) Do I have to keep "char source[]" in order to be able to return the array this way, or is there a way to use pointers in the last line too?

2) Could someone explain why we would want to use a pointer for the function name like used in my example? (the star before *mon_strcpy confuses me.)

Thanks!
0
 
LVL 17

Assisted Solution

by:rstaveley
rstaveley earned 200 total points
ID: 21846533
In C, you are told that arrays as passed "by reference" in function parameters. C++ has its own meaning for the word reference, which confuses matters, but is handling for function parameters is the same as C. Arrays are not passed by value. Instead a pointer to the array is passed.

You can do weird things with arrays when they are passed by reference, because they are actually just pointers.

The following pointer arithmetic is perfectly legal, because the parameter isn't really an array(!):

  void foo(int parm[100])
  {
        parm++;
        printf("2nd int is %d\n", *parm);
  }

That would be less shocking if you wrote:

  void foo(int* parm)
  {
        parm++;
        printf("2nd int is %d\n", *parm);
  }

It is actually the same, though. In both cases the parameter is passed as a pointer. This is what a C-programmer would call a reference [and a C++ programmer calls an embarrassing legacy].

It doesn't harm to think of a parameter as an array or a pointer. Both work.

You cannot return an array. You can only return a pointer to it. If C or C++ allowed functions apparently to return arrays, it would confuse the programmer, because all that can be returned is a pointer to something that needs to be put somewhere or is already stored somewhere. If you want a copy of an array to be returned from a function, you need to put it into a struct (or class). The fact that you cannot return an array if the language's polite way of telling you that you need to know "where to stick it" 8-)
0
 
LVL 11

Assisted Solution

by:spoxox
spoxox earned 100 total points
ID: 21846562
char *mon_strcpy(...)
means that the function returns a char pointer.

E.g.

char *mon_strcpy(char *destination, char *source) {
  char *temp = destination;
  /* copy */
  return temp;
}

You may recall some string functions work like this (updated parameter and return value the same) - e.g. strcpy (http://www.cplusplus.com/reference/clibrary/cstring/strcpy.html)
0
 
LVL 11

Assisted Solution

by:spoxox
spoxox earned 100 total points
ID: 21846604
If I point to the tables this way, will the last line on my code work?

I am speaking of the following line :

>> return destination;


This depends on what you do to "destination" in the function. If you update (locally to the function) it like in my post 21843242 above, it will be pointing to the end of the string and won't give the intended result. In this case, saving the beginning value as in 21846562, or alternatively recalculating the beginning value, would work.
0
 
LVL 5

Author Comment

by:codeQuantum
ID: 21846749
Thanks everyone!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

860 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