-1

I've searched and tried many other answers to problems like mine, but I still don't get it, I don't know what I'm missing here. This program should simulate an auction, it's a game which the smaller "bid" wins. I've come up with this code, but when I use the strcpy and the char variable type, this segmentation error comes up.

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

int func02(int venc, int nj, int max, int mod, float med, char *nome);
int lanc;

int main (void)
{

    lanc = func02(0, 36, 15, 0, 0, "nome");
    printf("%d", lanc);
    return EXIT_SUCCESS;
}

int func02(int venc, int nj, int max, int mod, float med, char *nome)
{

    int lance;

    if (venc == 0) {
        strcpy(nome, "ceadf");
        lance = 4;
    }
    else {
        if ((med <= mod) && (venc >= mod)) {
            lance = mod++;
        }
        else {
            lance = venc++;
        }
    }
    return(lance);
}
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
  • `lanc = func02(0, 36, 15, 0, 0, "nome");` and then `strcpy(nome, "ceadf");` you can't do this – Federico klez Culloca Sep 18 '17 at 07:10
  • Compile with all warnings & debug info (`gcc -Wall -Wextra -g` with [GCC](http://gcc.gnu.org/)...). **Use the debugger** `gdb` – Basile Starynkevitch Sep 18 '17 at 07:11
  • @RohanKumar please don't edit the style of the post, (like moving braces on their own line), it's not useful. – Federico klez Culloca Sep 18 '17 at 07:13
  • @FedericoklezCulloca: Ah, sorry. I would keep that in mind from next time ;) – Rohan Kumar Sep 18 '17 at 07:16
  • @BasileStarynkevitch I've compiled just like you've mentioned. The debugger I don't have because I'm using Putty to connect to a server running a Debian for educational purpose. Where do I paste the warnings from GCC? – eduardodinizf Sep 18 '17 at 07:24
  • You very probably have `gdb` on that Debian server. Correct your source code to have *no* warning at all. BTW you should consider installing some Linux distribution on your own computer (since Linux is great to learn programming, and later to do programming professionally) – Basile Starynkevitch Sep 18 '17 at 07:30
  • 'The debugger I don't have' - you should stop trying to write programs. You don't have enough tools. – Martin James Sep 18 '17 at 09:47

3 Answers3

3

You have passed string literal "nome" to func02() and your are writing it.

String literals are read only, writing to them will lead to undefined behavior like segmentation failt

Pras
  • 4,047
  • 10
  • 20
1

You cant use the function strcpy to copy the string into a string literal.

Instead dynamically allocate your string and then call strcpy().

qwn
  • 347
  • 3
  • 15
1

Okay, first, when you are posting questions like this, it's better to show what line the code is crashing on, and preferably a backtrace. With that said, here's what's going on:

lanc = func02(0, 36, 15, 0, 0, "nome");

Here you are sending the constant string "nome" to func02.

strcpy(nome, "ceadf");

Here you are trying to copy "ceadf" into nome, which fails for two reasons:

  1. nome is a constant string. You can't modify it.

  2. Even if you could modify it, it's only five characters long (each letter in "nome" plus the null byte), and you're trying to copy six bytes into it (each letter of "ceadf" plus a null byte). This would be a memory issue even if you were working with a writable string. In general, I'd avoid the use of strcpy, as it makes it easy to make mistakes like this.

Charles Srstka
  • 16,665
  • 3
  • 34
  • 60