Susano
Susano

Reputation: 284

Why do I get segmentation fault on trying to compare two strings?

I was working on an assignment for a simple assembler that should recognize arbitrary variable names like high programming languages. I tried to use Dynamic allocation to an array of char pointers

I am just trying to make an extensible array of strings and being able to search this array for specific strings But it gives a segmentation fault on the line of trying to compare the two strings [line: 14]

Comp(&buffer[1], Variables[i];
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define Comp(a,b)  strcmp(a,b) == 0 ? 1 : 0

char buffer[255], **Variables;
int VariableIndex;

void A_instructionHandler() {
    int A_Operand, test = 0;
    if (buffer[0]== '@') { 
        for (int i = 0; i <= VariableIndex; i++) {
            test = Comp(&buffer[1], Variables[i]);
            if (test) { 
                A_Operand = i + 16; 
                break;
            }   
        }   
    }   
}

int main(int argumentCounter, char *arguments[]) { 
    strcpy(buffer, "@variable");
    Variables = (char **)calloc(VariableIndex + 1, sizeof(char**));
        A_instructionHandler();
}

Upvotes: 1

Views: 317

Answers (1)

tadman
tadman

Reputation: 211610

Here's that code refactored into something more idiomatic:

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

void instruction_handler(char* buffer, char** variables) {
  switch (buffer[0]) {
    case '@':
      // This iterates over the NULL terminated array by stopping when
      // it hits a NULL pointer, or in other words *v is false.
      for (char **v = variables; *v; ++v) {
        // If this matches a variable name...
        if (!strcmp(*v, &buffer[1])) {
          // Variable matched, so show some debugging code
          printf("Found variable: %s\n", *v);
          return;
        }
      }
  }
}

int main(int argc, char* argv[] ) {
  // Create a simple NULL-terminated array of arbitrary size
  char *variables[] = {
    "variable",
    NULL
  };

  instruction_handler("@variable", variables);
}

Where that variables array can be defined however you like, extended, shrunk, so long as the NULL terminator remains in place at the end.

Some tips based on your original code:

  • Don't use global variables unless you have an extremely compelling reason. No such reason existed here.
  • Make your functions clear in intent and purpose.
  • Pick a naming convention and stick to it.
  • Use C conventions like strcmp() and just deal with how weird it is, don't wrap that in a #define and invent your own C dialect nobody understands. You'll get used to C over time, it won't bother you as much, and you can code without driving other people on your team up the wall.
  • Explanations like argc expanded to argumentCounter is better expressed as a comment than an awkwardly long variable name, especially one that's very non-standard. Comments are great. Use them as much as you like!

Upvotes: 6

Related Questions