C - Errors / Warnings - After compiling

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.

1. Assignment discards qualifiers from pointer target type

A. The problem

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 *.

B. The solution

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.

2. Warning: assigment makes pointer from integer without a cast

A. The problem

Sometimes you can be warned by your shell with this error:

warning: assigment makes pointer from integer without a cast.

B. The solution

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

3. functions.c:86: error: expected ‘)’ before ‘*’ token make: *** [functions.o] Error 1

A. The problem

It is often because you forgot to include a file, such as an header.h for example.

B. The solution

Include the header file in the correct source file:

#include    "myHeader.h"

 

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.