Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good morning

I wonder how to fix my program so that it copies array A to array A2 in the function "copyarraystringto()" and returns the A2 string to the function printf() which should display the string in array A2?

// Jones, Bradley L.; Peter Aitken; Dean Miller. C Programming in One Hour a Day, Sams Teach Yourself (pp. 400). Pearson Education. Kindle Edition.
//
/* Exercise 5, lesson 10.  */

  #include <stdio.h>

  char A[18] = { "Pointers are fun!" };
  char A2[18];

  char copyarraystringto(char Ac[], char A2c[]);

  int main( void )
  {

     printf("%s", copyarraystringto(A, A2));

     return 0;
 }

char copyarraystringto(char Ac[], char A2c[])
{

    strcpy(A2, A);

    return A2;
}


What I have tried:

I have tried to use the "Debug" in CodeBlocks 20.03 and there was a problem in this line:
printf("%s", copyarraystringto(A, A2));
. Something about memory. This info from CodeBlocks:
Cannot access memory at address 0x0
.
Posted
Updated 6-Sep-21 7:30am

First off, that won't compile because copyarraystringto returns a char rather than a pointer.
char *copyarraystringto(char Ac[], char A2c[])


You pass parameters to your function:
char copyarraystringto(char Ac[], char A2c[]);
...
printf("%s", copyarraystringto(A, A2));

But you don't use them:
char copyarraystringto(char Ac[], char A2c[])
{

    strcpy(A2, A);

    return A2;
}
So you copy the empty global string A2 into the full global string A and then return it - so you print an empty string.

Use the parameters instead:
char *copyarraystringto(char Ac[], char A2c[])
{

    strcpy(A2c, Ac);

    return A2c;
}
And it'll work.

But please, use sensible variable names! It makes code a lot easier to read and work with later...
char copyarraystringto(char source[], char dest[])
{

    strcpy(source, dest);

    return dest;
}
 
Share this answer
 
v2
Comments
Mieczyslaw1683 6-Sep-21 6:29am    
Aha, Interesting. One thing that is weird is that it feels like you are mixing the parameters of function "strcpy()". The right array goes to the left array. So I think it is like this:
strcpy(dest, source);
when it is more logical.
In my opinion, that should be:
C
#include <stdio.h>
#include <string.h>


int main()
{
  char source[] = { "Pointers are fun!" };
  char target[sizeof(source)];

  printf( "%s\n", strncpy(target, source, sizeof(source)));

  return 0;
}
 
Share this answer
 
In C your are responsible for memory managment and so you should do with functions. A simple example
C++
char* createString() {
  char *s = malloc(/*some size*/);
  // do stuff
  return s;
}
// usage
char *text = createString();
// do further stuff
  free( text );
hint: I use create WHEN allocating memory, so the callee knows that free is his job.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900