1

I am currently in , implementing sort. I chose to do selection sort and at the end of it I need to swap the numbers, but when I try to compile, I get this message:

implicit declaration of function 'swap' is invalid in C99 [-Werror,
-Wimplicit-function-declaration]

I included stdio.h already and I don't know what to do anymore. here's my code:

[hidden for academic honesty]

  • Swap is not a function from stdio.h. You need to define it yourself. Can I see your swap code? – i_am_david Jul 23 '15 at 16:16
  • I still don't know how to define it, it isn't covered on week 3. Is it covered on week 4? – Talita Maria Briganti Barbosa Jul 23 '15 at 17:41
  • Creating functions was covered in week 1 or 2... – i_am_david Jul 23 '15 at 18:32
0

I'm not 100% sure but I think it is because you haven't declared the function before main (you know the prototype). I've written some example code to show how to do it if that is the problem:

#include <cs50.h>
#include <stdio.h>

//Tells the compiler that these are the name of functions (along with their parameters)
//and they will be defined at some point later in the code
char* getName();
void displayName(char* name); 

int main(){

char* name = getName();
displayName(name);
return 0;
}

//Gets the name from the command line
char* getName(){
    char* name;
    printf("Enter your name: ");
    name = GetString();
    return name;

}

//displays name
void displayName(char* name){
    printf("Your name is: %s\n", name);

}
| improve this answer | |

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .