-4

When I try and use atoi with an int and malloc I get a bunch of errors and key is given the wrong value, what am I doing wrong?

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

struct arguments {
    int key;
};

void argument_handler(int argc, char **argv, struct arguments *settings);

int main(int argc, char **argv) {
    argv[1] = 101; //makes testing faster
    struct arguments *settings = (struct arguments*)malloc(sizeof(struct arguments));
    argument_handler(argc, argv, settings);
    free(settings);
    return 0;
}

void argument_handler(int argc, char **argv, struct arguments *settings) {
    int *key = malloc(sizeof(argv[1]));
    *key = argv[1];
    settings->key = atoi(key);
    printf("%d\n", settings->key);
    free(key);
}
aLongBoat
  • 59
  • 9
  • 1
    most obvious thing you're doing that is odd/wrong is that `atoi` takes a `char *` as it's only parameter and you're passing it an `int *` – Chris Turner Dec 12 '19 at 12:35
  • 1
    *"I get a bunch of errors"* - What errors? – klutt Dec 12 '19 at 12:36
  • Compile with all warnings enabled and consider them as errors. – Jabberwocky Dec 12 '19 at 12:37
  • although assigning `101` to `argv[1]` is also pretty weird...what is at memory location 101 that you think is a string? – Chris Turner Dec 12 '19 at 12:38
  • Why use malloc in argument handler? Just do `settings->key = atoi(argv[1])` However, atoi is a function that should not be used (google why). – klutt Dec 12 '19 at 12:41
  • 1
    `argv[1] = 101;` makes no sense at all. – Weather Vane Dec 12 '19 at 12:42
  • Tbh, this very short code is full with small details that proves that you need to go back pretty far and learn the basic concepts, like string handling and memory allocation. – klutt Dec 12 '19 at 12:43
  • @klutt I suppose `argument_handler` is still to elaborated. – Jabberwocky Dec 12 '19 at 12:46
  • 3
    When I compile this with `-Wall -Wextra` I get four warnings, which is a lot for such a short snippet. Activate warnings. Those four warnings are all things that points out flaws in your code, but there are more things too. – klutt Dec 12 '19 at 12:46
  • 2
    And [don't cast malloc](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – klutt Dec 12 '19 at 12:47

1 Answers1

0

You probably want this:

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

struct arguments {
  int key;
};

void argument_handler(int argc, char** argv, struct arguments* settings);

int main(int argc, char** argv) {
  argv[1] = "101";  // 101 is a string, therefore you need ""
  struct arguments* settings = (struct arguments*)malloc(sizeof(struct arguments));
  argument_handler(argc, argv, settings);
  free(settings);
  return 0;
}

void argument_handler(int argc, char** argv, struct arguments* settings) {
  char* key = malloc(strlen(argv[1]) + 1);  // you want the length of the string here,
                                            // and you want char* here, not int*

  strcpy(key, argv[1]);                    // string needs to be copied
  settings->key = atoi(key);
  printf("%d\n", settings->key);
  free(key);
}

But this is very awkward, actually the argument_handler can be rewritten like this:

void argument_handler(int argc, char** argv, struct arguments* settings) {
  settings->key = atoi(argv[1]);
  printf("%d\n", settings->key);
}

Disclaimer: I only corrected what was obviously wrong, there are still checks that need to be done, e.g. if argc is smaller than 2 etc.

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115