I am practicing with pointers presently, trying to get my knowledge on the matter more robust.
I created one program that just uses 2 points and another that uses on string and one array.
Both compile and work, but I get a warning with the program that uses just pointers. Can someone explain why this is the case. It would be much appreciated.
program with pointer and array:
Code:
#include <stdio.h>
#include <stdlib.h>
char *pcopy(const char *s, char t[]);
int main(void){
char *stringp = "String";
char stringp2[8];
pcopy(stringp, stringp2);
printf("\n%s", stringp2);
return 0;
}
char *pcopy(const char *s, char t[] ){
char *p = t;
while(*s != '\0' )
{
*p++ = *s++;
}
*p = '\0';
return t;
}
The above works.
But the following program compiles and runs but there is an error...
Code:
#include <stdio.h>
#include <stdlib.h>
char *pcopy(const char *s, char *t);
int main(void){
char *stringp = "String";
char *stringp2[8];
pcopy(stringp, stringp2);
printf("\n%s", stringp2);
return 0;
}
char *pcopy(const char *s, char *t ){
char *p = t;
while(*s != '\0' )
{
*p++ = *s++;
}
*p = '\0';
return t;
}
The error is :
ecl.c: In function ‘main’:
ecl.c:12: warning: passing argument 2 of ‘pcopy’ from incompatible pointer type
Where exactly am I going wrong here?
Also my is it that if I change:
Code:
char *stringp2[8];
to
char *stringp2;
I get a segmentation fault...Is it wrong when using pointer to define a pointer as:
Sorry if this post is a little confused I am still hazy in regards to pointers.