Errors and warnings generally encountered by a coder during his hard life of coder will be added here, in this special section.
I will not make difference between errors and warnings, because I always compile with CFLAGS such as -Wall -Werror -Wextra and thus warnings are treated as errors.
These errors will be those appearing after compiling a program.
So we assume that we will just try to compile and of course it will fail.
I try to add a char * in another char * with the "=" operator.
#include <stdio.h> #include <stdlib.h> #include "string.h" typedef struct { char *name; } theStruct; /** * INIT */ void stringInit(theStruct *this, char const *s) { this->name = malloc(sizeof(char *) * strlen(s) + 1); this->name = s; } /** * MAIN */ int main(void) { theStruct *myStruct; myStruct = malloc(sizeof(theStruct *)); stringInit(myStruct, "Hello"); printf("myStruct->name = %s\n", myStruct->name); return (0); }
The problem may also be that you try to manipulated a const char * instead of a simple char *.
I now use the strcpy() function to add the char * into another one.
#include <stdio.h> #include <stdlib.h> #include "string.h" typedef struct { char *name; } theStruct; /** * INIT */ void stringInit(theStruct *this, char const *s) { this->name = malloc(sizeof(char *) * strlen(s) + 1); strcpy(this->name, s); } /** * MAIN */ int main(void) { theStruct *myStruct; myStruct = malloc(sizeof(theStruct *)); stringInit(myStruct, "Hello"); printf("myStruct->name = %s\n", myStruct->name); return (0); }
Try also to remove the const before the char * or add it if you need it.
Sometimes you can be warned by your shell with this error:
warning: assigment makes pointer from integer without a cast.
It is often because you forgot to include a library in your file.
Do a man of your system function to see which libraries to include:
$ man yourSystemFunction
It is often because you forgot to include a file, such as an header.h for example.
Include the header file in the correct source file:
#include "myHeader.h"
Add new comment