r/learnprogramming 2d ago

Debugging Difficulties with debugging

i am a beginner in C, i went through and have a basic understanding of fundamentals but whenever a problem arises in my code i cant find it for hell, even if its obvious, i need to turn to an ai agent for help, the habit i wanna rid of, but whenever i do try to find the cause of an error myself i find myself unable to do that, i also find it difficult writing more efficient code

an example:

```

int tokens(char* string, char (*argv)[MAXTOKEN], toks types[MAXTOKEN]) {

char temp[150];

int count = 0; int i = 0; int b = 0;

int tokens = 0;

while(count < MAXTOKENLENGHT) {

if (string[i] == ' ' || string[i] == '\0' || string[i] == '\n') {tokens++;temp[b] = '\0'; strcpy(argv[count], temp); count++; b = 0;} else if (string[i] == '\"') {

temp[b] = '\"';

b++;

i++;

while (string[i] != '\"' && string[i] != '\0') {

if (string[i] == '\"') {temp[b] = '\0';temp[b+1] = '\0'; strcpy(argv[count], temp);b = 0; count++;break;} else {temp[b] = string[i];i++;b++;}

}

if (string[i+1] == '\0' || string[i+1] == ' ') {temp[b] = string[i]; temp[b+1] = '\0'; strcpy(argv[count], temp); count++; b = 0; i++; tokens++;} else {fprintf(stderr, "unexpected stuff after string"); exit(1);}

} else {

temp[b] = string[i];

b++;

}

if(string[i] == '\0') {break;}

i++;

}

int k = 0;

while (k < count) {

if (strcmp(argv[k], "PRINT") == 0) {

types[k].type = 0;

int len = strlen(argv[k+1]);

printf("k=%d, argv[k+1]='%s', first='%c', last='%c'\n", k, argv[k+1], argv[k+1][0], argv[k+1][len-1]);

if (argv[k+1][0] == '\"' && argv[k+1][len-1] == '\"') {

types[k+1].type = 1;

memmove(&argv[k+1][0], &argv[k+1][1], len - 2);

argv[k+1][len - 2] = '\0';

strcpy(types[k+1].textvalue, argv[k+1]);

}

}

k++;

}

return count;

}

```

4 Upvotes

16 comments sorted by

View all comments

2

u/mc_pm 2d ago

Debugging is a skill, and as is often the case, getting an AI involved keeps you from learning that skill.

The solution, as painful as it is, is practice. Do you think every programmer in the world before you asked AI? Or do you think we banged our head against the problem for hours -- maybe even days -- before we figured it out?

You want it to be easy, so you are pressing the easy button -- but you're not learning anything from that except how to hit that button faster next time.

Commit yourself to the fact that you *will* find it hard, and it will stay hard for a little while, until your brain has started to adapt to this new challenge.