Solved

Switching from char tables to pointers

Posted on 2008-06-22
15
274 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
Comment Utility
you can change all
source[index] to *(source+index)
0
 
LVL 5

Author Comment

by:codeQuantum
Comment Utility
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
Comment Utility
char *mon_strcpy(char *destination[], char *source) {
0
 
LVL 13

Assisted Solution

by:josgood
josgood earned 50 total points
Comment Utility
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
Comment Utility
sorry, [] should have been deleted
0
 
LVL 13

Expert Comment

by:josgood
Comment Utility
>>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
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 84

Expert Comment

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

Expert Comment

by:spoxox
Comment Utility
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
Comment Utility
> 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks everyone!
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
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…
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 viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

763 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

6 Experts available now in Live!

Get 1:1 Help Now