Return string copy using pointers?

Hello, the exercise in my book is very clear, write a function that returns the copy of a certain string using pointers. I wrote the function, but I don't know how to call it to test it if I've done it right or not. Here's my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <stdio.h>
#include "stdafx.h"
#include <conio.h>
#include <string.h>
#include <stdlib.h>
int main ()	{
	
	char string[256];
	printf("Enter your string below\n");
	scanf("%s", &string);

	// here I need to call the function
	getch();
			}

//Function below
char *strcpy (char *s)
{
	int l = strlen(s);
	char *s2, *s3;
	s2 = (char*)s3;
	s2 = (char*)malloc(sizeof(char)*l + 1);

	if (!s2) return "";

	s3 = s2;
		for (*s; s++; s3++)
			*s3 = *s;
		
		return s2;
}
It is simply to do by comparing the source string with the built string.


1
2
3
4
5
	// here I need to call the function
	char *new_string = strcpy( string );
	if ( strcmp( string, new_string ) == 0 ) puts( "The strings are equal." );
	else puts( "Oops. There is an error somewhere." );
	getch();

Last edited on
You need a function prototype @line 5/6:
char* strcpy(char *s);

Or move the whole function to before main().

I'm not sure that the "&" in line 10 is correct, "string" would already be an address.
Last edited on
Topic archived. No new replies allowed.