[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 288
  • Last Modified:

Switching from char tables to pointers

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
codeQuantum
Asked:
codeQuantum
  • 4
  • 4
  • 3
  • +2
6 Solutions
 
ozoCommented:
you can change all
source[index] to *(source+index)
0
 
codeQuantumAuthor Commented:
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
 
ozoCommented:
char *mon_strcpy(char *destination[], char *source) {
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
josgoodCommented:
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
 
ozoCommented:
sorry, [] should have been deleted
0
 
josgoodCommented:
>>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
 
codeQuantumAuthor Commented:
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
 
ozoCommented:
the way it is being used, it looks like an error to me
0
 
spoxoxCommented:
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
 
rstaveleyCommented:
> 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
 
codeQuantumAuthor Commented:
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
 
rstaveleyCommented:
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
 
spoxoxCommented:
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
 
spoxoxCommented:
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
 
codeQuantumAuthor Commented:
Thanks everyone!
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.

  • 4
  • 4
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now