1

I made this header for some program functions:

#ifndef STRING_H_INCLUDED
#define STRING_H_INCLUDED

#include <stdio.h>

int s_length(char *test_string){      //returns string length
    int i=0;
    while(*test_string){
        i++;
        test_string++;
    }
    return i;
};

void s_insert(char *string_one){        //inserts string
    scanf("%s",string_one);
};

void s_output(char *string_one){        //outputs string
    printf("%s",string_one);
};

#endif

and I call the functions in the c file like 2 times each. But the last 2 of them get this: warning: implicit declaration of function ‘s_insert’ and undefined reference to 's_insert'. for both functions.

What does this mean, and what did I do wrong?

It might have to do with the main c file in which I call the functions.

main progam:

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

char *name,*surname;


void menu(){
    int choise;
    do{
        printf("1: incerici dati\n");
        printf("2: output dati\n");
        printf("3: calcola lungezza\n");
        printf("0: ecsi\n");
        printf("incerici: ");
        scanf("%d", &choise);
        printf("------------------\n");
        switch(choise){
            case 1:
                printf("String 1:");
                s_insert(name);
                printf("String 2:");
                s_insert(surname);
                printf("------------------\n");
                break;
            case 2:
                s_output(name);
                s_output(surname);
                printf("------------------\n");
                break;
            case 3:
                printf("string 1: %s  lungezza: %d \n",name,s_length(name));
                printf("string 2: %s  lungezza: %d \n",surname,s_length(surname));
                printf("------------------\n");
                break;
            case 0:
                printf("prgram closed!!\n");
                break;
            default:
                printf("Errore: %d schelta invalida\n",choise);
                break;
        }
    }while(choise);
};

int main(int argc, char *argv[]){
    name=malloc(sizeof(char)*20);
    surname=malloc(sizeof(char)*30);
    menu();
    return 0;
}

ps fot the people wondering, the printed text is italian because it's a school exercice.

BRHSM
  • 854
  • 3
  • 13
  • 48
  • 1
    why'd you put function definitions in header? – Sourav Ghosh Feb 18 '15 at 10:30
  • @SouravGhosh I followed a tutorial on using headers and this is how he wold me to do it – BRHSM Feb 18 '15 at 10:31
  • are you sure `string.h` and `main.c` are in same folder?Its working for me.Make sure you typed names correctly. – Vagish Feb 18 '15 at 10:33
  • it works for me, but you need to cast the malloc result to char* – swang Feb 18 '15 at 10:35
  • 2
    @swang Check this `http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc` for why you should not cast the result of malloc. – Santosh A Feb 18 '15 at 10:36
  • @wsang what does that mean – BRHSM Feb 18 '15 at 10:37
  • 7
    Don't call your header `string.h` , it's the same name as a C library header which contains, for example, strlen/strcat/etc. Rename your header to something else, like `my_string.h` – Bulletmagnet Feb 18 '15 at 10:38
  • yeah no, I changed the name because the headerr is my first name which I dont want to lay around the internet so i changed it – BRHSM Feb 18 '15 at 10:40
  • i tried: `s_insert(&name);` instead of `s_insert(name);` but no succes – BRHSM Feb 18 '15 at 10:47
  • @CoderGuy: by the way this is a pure C question, you may want to edit the tag so it's not c++ – swang Feb 18 '15 at 10:48
  • I thought I deleted that but I'll take care of it now – BRHSM Feb 18 '15 at 10:54
  • @SantoshA neither clang nor gcc4.9 will compile it without the cast: error: assigning to 'char *' from incompatible type 'void *' -- I just realised I was saving the file in .cpp so it's compiled as c++, it's fine in c, but in c++ it won't compile without the cast. – swang Feb 18 '15 at 10:55
  • This should give either an error for all three functions (including `s_length`) or for none of them. Please make sure the code is copied and pasted from code you've tried exhibiting the described behavior, and include the compiler output verbatim. – mafso Feb 18 '15 at 11:25
  • OT: This `s_insert` function is inherently broken. No matter how much memory `string_one` points to, the user could provide more input. (Look for the `gets` function which was removed from the language for exactly the same reason, for example, for further explanation.) – mafso Feb 18 '15 at 11:29
  • The problem is not in the code you are showing us – zoska Feb 18 '15 at 13:09
  • @zoska than what is it? – BRHSM Feb 18 '15 at 16:49

2 Answers2

1

I compiled your code in .c file and compiled it. I was hoping to get error as

string.h : No such file or directory

but,as string.h is also a name of standard C library compiler gave me

[Warning] implicit declaration of function 's_insert' [-Wimplicit-function-declaration]

Hope above explanation gives you a clue.

Most probably:

  1. Your header and source are not in same folder.

OR

  1. Your include path not set to correct location

OR

3.You are having other name to header than string.h

Note: Copying your header code into string.h created by me works well.

enter image description here

Vagish
  • 2,520
  • 19
  • 32
0

It's very likely that the string.h that got included was not "your" string.h but the C library's string.h. I'm only guessing because you didn't show us how you invoked the compiler.

You should rename your header file.

Also, you shouldn't define functions in a header, only declare them:

int s_length(char *test_string);      //returns string length
void s_insert(char *string_one);      //inserts string

The implementation should be in another c file (what you have in your header file now). Then you should build the main file and the file implementing your functions, together:

$ gcc -Wall -pedantic -W main.c functions.c

If you are still getting the warning: implicit declaration of function ‘s_insert’ and undefined reference to 's_insert' messages, that means that your header (whatever its name) was not processed by the compiler. If you did not get a fatal error: string.h: No such file or directory message, that means another file with the same name was found (that's why string.h is a bad idea for your own file). To check what files are included, add the -H switch to GCC (or /showIncludes if you are using Microsoft Visual Studio).

Bulletmagnet
  • 5,665
  • 2
  • 26
  • 56
  • the header is not called that way but I just called it so you guys dont know my real name – BRHSM Feb 18 '15 at 10:52
  • so do I have to make another c file and than main.c is my main code and functions.c is the new file. – BRHSM Feb 18 '15 at 10:56
  • That would be the right thing to do. Your attempt with main.c and one header would work for this small example, but it's a bad habit to do it that way. – Bulletmagnet Feb 18 '15 at 11:03