简体   繁体   中英

Is there a way to check if the pointer has a faulty value?

So me and people here have reached the conclusion that the variable probably contains a faulty value, I was asked to post the rest of the code, however my question was not meant to be how to fix my code(its quite enourmous anyway) but if I can do a check for faulty value that is not null(if there is such thing in c)

char *s;
    s = search(arr1[0], arr2[0]);

this never happens

 if(s==NULL) printf("Not found");

yet this crashes the console if NULL was meant to be returned

 strlen(s)

here is the code

typedef struct PERSON {
    char name[25];
    char surname[36];
    char rnum[11];
    char adress[50];
} PERSON;

int compare2(const void* kl, const void* oso) {
    PERSON* par = (PERSON*)oso;
    char key1[60];
    strncpy_s(key1, 35, par->surname, 35);
    strncat_s(key1, 24, par->name, 24);
    return strcmp((const char*)kl, (const char*)key1);
}



   char* search(const char* jm, const char* pr) {
        char key[60];
        PERSON* pom;
        strncpy_s(key, 60, pr, 35);
        strncat_s(key, 60, jm, 24);
        pom = (PERSON*)bsearch(key, registr, countPerson, sizeof(PERSON), compare2);
        return pom->rnum;

to show everyone what the solution is clearly for display, here is it

  char* search(const char* jm, const char* pr) {
        char key[60];
        PERSON* pom;
        strncpy_s(key, 60, pr, 35);
        strncat_s(key, 60, jm, 24);
        pom = (PERSON*)bsearch(key, registr, countPerson, sizeof(PERSON), compare2);
if (!pom)
        return NULL;
    else
return pom->rnum;

I'm writing an answer for code readability

The fact that s != NULL doesn't mean much.
s could have a faulty value, be != NULL , and still cause your crash.
I've tried to write something out to help the OP in providing a minimal reproducible example, but this code is not meant to work

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

typedef struct PERSON {
    char name[60];
    char surname[60];
    char rnum[60];
    char adress[60];
} PERSON;

int compare2(const void* kl, const void* oso) {
    PERSON *par = (PERSON*)oso;
    PERSON *key = (PERSON*)kl;
    int cmp;

    cmp = strcmp(par->surname, key->surname);
    if (cmp)
        return cmp;
    else
        return strcmp(par->name, key->name);
}

char *search(const char* jm, const char* pr) {
    PERSON key;
    PERSON *pom;
    strncpy_s(key.surname, 60, pr, 35);
    strncpy_s(key.name, 60, jm, 24);
    pom = (PERSON*)bsearch(&key, registr, countPerson, sizeof(PERSON), compare2);
    if (!pom)
        return NULL;
    return pom->rnum;
}

int main()
{
    char *arr1[2] = { "arr1[0]", "arr1[1]" };
    char *arr2[2] = { "arr1[0]", "arr1[1]" };
    char *str = search(arr1[0], arr2[0]);
    
    if (str == NULL)
        printf("Not found\n");
    else
        printf("strlen(%s): %lu\n", str, strlen(str));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM