I just started to look at C, coming from a java background. I’m having a difficult time wrapping my head around pointers. In theory I feel like I get it but as soon as I try to use them or follow a program that’s using them I get lost pretty quickly. I was trying to follow a string concat exercise but it wasnt working so I stripped it down to some basic pointer practice. It complies with a warning conflicting types for strcat function and when I run it, crashes completly.
Thanks for any help
#include <stdio.h>
#include <stdlib.h>
/* strcat: concatenate t to end of s; s must be big enough */
void strcat(char *string, char *attach);
int main(){
char one[10]="test";
char two[10]="co";
char *s;
char *t;
s=one;
t=two;
strcat(s,t);
}
void strcat(char *s, char *t) {
printf("%s",*s);
}
Your
printf()should look like this:The asterisk is unnecessary. The
%sformat argument means that the argument should be achar*, which is whatsis. Prefixingswith*does an extra invalid indirection.You get the warning about conflicting types because
strchris a standard library routine, which should have this signature:Yours has a different return type. You should probably rename yours to
mystrchror something else to avoid the conflict with the standard library (you may get linker errors if you use the same name).