Hi,

I am writing a reverse function, to reverse a string, but when I am compiling I am getting an error, and I know its from the malloc line of code. This is the first time i have used malloc, but I want the function to work for any length of string.

#include <stdio.h>
#include <stdlib.h>
//reverse a string
int mystrlen(char *s);
void reverse(char *s);

int main() {
	char string1[80];
	printf("Enter string:");
	gets(string1);
	reverse(string1);
	printf("%s",string1);
}
int mystrlen(char *s) {
	int i=0;
	while (s[i] != '\0') {
		i++;
	}
	return i;
}
void reverse(char *s) {
	int i=0, length;
	char *p;
	length = mystrlen(s);
	p = malloc(length+1); //this line here 
	while (s[i] != '\0') {
		p[i] = s[length-(i+1)];
		i++;		
	}
	s[i] = '\0';
	free(p);
}

my understanding is that malloc will return a pointer. In my case p.
I declared char *p as a character, so p is of char type and a pointer to the char.

This is the error I get:
In function `void reverse(char*)':
error: invalid conversion from `void*' to `char*'

instead of

p = malloc(length+1);

use

p = (char *) malloc(length+1);

that is not a error, its a warning from the compiler.

because malloc returns pointer to void type

i.e

void * malloc(unsigned long );

(have a look at manual page of malloc for more info if you are using GCC compiler)

so that address you need to type cast to your char * and then store in the variable.

A piece of working code:

#include <stdio.h>
#include <stdlib.h>

int mystrlen(char *s);
char* reverse(char *s);

int main() {
	char *string;
	string=(char*)malloc(sizeof(char)*50);

	printf("Enter string:");
	scanf("%s",string);
	printf("Input string: %s \n",string);

	string=reverse(string);

	printf("Output String %s \n",string);

	return (0);
}
int mystrlen(char *s) {
	int i=0;
	while (s[i] != '\0') {
		i++;
	}
	return i;
}
char* reverse(char *s) {
	int i=0, length;
	char *p;
	length = mystrlen(s);
	p = #include <stdio.h>
#include <stdlib.h>

int mystrlen(char *s);
char* reverse(char *s);

int main() {
	char *string;
	string=(char*)malloc(sizeof(char)*50);

	printf("Enter string:");
	scanf("%s",string);
	printf("Input string: %s \n",string);

	string=reverse(string);

	printf("Output String %s \n",string);

	return (0);
}
int mystrlen(char *s) {
	int i=0;
	while (s[i] != '\0') {
		i++;
	}
	return i;
}
char* reverse(char *s) {
	int i=0, length;
	char *p;
	length = mystrlen(s);
	p = (char*)malloc((length+1)*sizeof(char));
	while (s[i] != '\0') {
		p[i] = s[length-(i+1)];
		i++;
	}
	s[i] = '\0';
	return p;
}

	while (s[i] != '\0') {
		p[i] = s[length-(i+1)];
		i++;
	}
	s[i] = '\0';
	return p;
}

Observations:
1) Remove the "//" comments, instead of them use: "/* */" comments. They are more portable.
2) If you want a void function to reverse your string then the signature should've been: "void reverse(char**)" . If you are not confortable with pointers, as an workaround you can declare your function "char* reverse(char*)" and use it this way: "string=reverse(string);".
3) Don't forget that your main() function should always return something, "return (0)".
4) In my code the "(type*)malloc(sizeof(type)*size)" is not compulsory... I am just accustomed to do that this way, probably from highschool.

Using scanf for strings, you will run into trouble if your input string contains whitespace. Only the first word will be reversed.

Also, in your reverse function, you are assigning the null incorrectly - it should be:

p[i] = '\0';

and not

s[i] = '\0';

Also, you should probably use another char* variable (as the return value) for your call to the reverse function in main. That way you don't destroy the original input string and you don't end up with a memory leak. Remember to free any dynamic allocated memory as well.

instead of

p = malloc(length+1);

use

p = (char *) malloc(length+1);

In C++ the cast is necessary, in C prefer not to cast.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.