C程序 - 警告:赋值使整数指针不带强制转换

时间:2017-02-14 10:20:01

标签: c

我试图读取文件并将其内容存储在变量中,这是我的代码:

#define _BSD_SOURCE
#include <stdio.h>
#include <sys/stat.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>

// CEK ROUTER MODEL
char* router_model;
char* model() {
    char filename[] = "/tmp/cpuinfo";
    char* key = "system type";
    char* value;
    FILE *file = fopen(filename, "r");

    if (file != NULL) {
        char line[1000];

        while (fgets(line, sizeof line, file) != NULL) /* read a line from a file */ {
            //fprintf(stdout, "%s", line); //print the file contents on stdout.
            if (strncmp(line, key, strlen(key)) == 0) {
                char* value = strchr(line, ':');
                value += 2;
                router_model = strdup(value);
                break;   // once the key has been found we can stop reading
            }
        }
        fclose(file);
    }
    else {
        perror(filename); //print the error message on stderr.
    }
    return router_model;
}

// TULIS SERIAL NUMBER KE FILE
char tulis(char p[100]) {
    // Write a serial number to a file
    char sn[30];
    char encrypt_sn[300];
    printf("Serial Number:\n");
    scanf("%s", sn);
    FILE *f = fopen("/tmp/halo/fsn-55cfc8770b69cc07268fae7f25ee444c", "w");
    if (f == NULL) {
        printf("Error opening file!\n");
        exit(1);
    }
    fprintf(f,"Serial Number: %s", sn);
    fclose(f);
    sprintf(encrypt_sn, "ccrypt -e /tmp/halo/fsn-55cfc8770b69cc07268fae7f25ee444c -K %s", p);
    system(encrypt_sn);
    system("mv /tmp/halo/fsn-55cfc8770b69cc07268fae7f25ee444c.cpt /tmp/halo/fsn-55cfc8770b69cc07268fae7f25ee444c");
    printf("Serial number is saved in /tmp/halo/fsn-55cfc8770b69cc07268fae7f25ee444c\n");
    return 0;
}

// BACA SERIAL NUMBER & SIMPAN DALAM SEBUAH VARIABLE
char baca(char p[100]) {
    // Store the serial number from a file in a variable
    char line[50];
    char decrypt_sn[300];
    char key[30] = "Serial Number";
    char *serial_number;
    if( access( "/tmp/halo/fsn-55cfc8770b69cc07268fae7f25ee444c", F_OK ) != -1 ) {
        system("cp /tmp/halo/fsn-55cfc8770b69cc07268fae7f25ee444c /tmp/");
        system("mv /tmp/fsn-55cfc8770b69cc07268fae7f25ee444c /tmp/fsn-55cfc8770b69cc07268fae7f25ee444c.cpt");
        sprintf(decrypt_sn, "ccrypt -d /tmp/fsn-55cfc8770b69cc07268fae7f25ee444c.cpt -K %s", p);
        system(decrypt_sn);
        FILE *file = fopen("/tmp/fsn-55cfc8770b69cc07268fae7f25ee444c", "r");
        if (file == NULL) {
            printf("Error opening file!\n");
            exit(1);
        }
        while (fgets(line, sizeof line, file) != NULL) /* read a line from a file */ {
            //fprintf(stdout, "%s", line); //print the file contents on stdout.
            if (strncmp(line, key, strlen(key)) == 0) {
                char* value = strchr(line, ':');
                value += 2;
                serial_number = strdup(value);
                break;   // once the key has been found we can stop reading
            }
        }
        fclose(file);
        //printf("Your hardware serial number is: (%s)\n", serial_number);
        remove("/tmp/fsn-55cfc8770b69cc07268fae7f25ee444c");
    }
    else {
        printf("fsn not found\n");
        return -1;
    }
    return 0;
}

int main(int argc, char* argv[]) {
    char *r;
    char *del;
    char *decrypt;
    int ret;
    char input[30];
    char *p;
    char *original_sn;
    p = "MmI4MTUxM2FjMjRlMDkzYmRkZGQyMjcwMjQ4OWY3MDAwNGZiYTM0MWNkZGIxNTdlYzAxN2";
    //tulis(p);
    original_sn = baca(p);
    printf("SN: %s\n", original_sn);
    return 0;
}

该文件为/tmp/halo/fsn-55cfc8770b69cc07268fae7f25ee444c,该文件的内容为Serial Number: 1866203214226041original_sn应输出1866203214226041。但是,当我运行该代码时,我得到:

test.c: In function ‘main’:
test.c:105:14: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
  original_sn = baca(p);
              ^
SN: (null)

我该如何解决?

3 个答案:

答案 0 :(得分:3)

这是因为您的baca函数返回char,而您将其返回值分配给char *。也许您想使用char变量。

答案 1 :(得分:1)

如果函数baca 可以更改输入参数指向的内存块的内容:

改变这个:

char* p = "MmI4MTUxM2FjMjRlMDkzYmRkZGQyMjcwMjQ4OWY3MDAwNGZiYTM0MWNkZGIxNTdlYzAxN2";

对此:

char p[] = "MmI4MTUxM2FjMjRlMDkzYmRkZGQyMjcwMjQ4OWY3MDAwNGZiYTM0MWNkZGIxNTdlYzAxN2";

如果函数baca 无法更改输入参数指向的内存块的内容:

改变这个:

char baca(char p[])

对此:

char baca(const char* p)

答案 2 :(得分:0)

在baca中,您使用strdup分配初始化内存: serial_number = strdup(value); 那你就什么都不做了。 很明显,您认为该函数返回指向该内存的指针,以便您可以打印它的内容。但是,这不是你在做什么。因为你的所有baca函数正在做的是返回一个值,如果它是sucseede(0)或不是(-1)。你是jut忽略那个指针并留下你的prog分配的一些浪费的未使用的内存。 它们是修复代码的2种方法:

方法1:返回serial_number

char* baca(const char* p) {
    // Store the serial number from a file in a variable
    char line[50];
    char decrypt_sn[300];
    char key[30] = "Serial Number";
    char *serial_number=NULL;
    if( access( "/tmp/halo/fsn-55cfc8770b69cc07268fae7f25ee444c", F_OK ) != -1 ) {
        system("cp /tmp/halo/fsn-55cfc8770b69cc07268fae7f25ee444c /tmp/");
        system("mv /tmp/fsn-55cfc8770b69cc07268fae7f25ee444c /tmp/fsn-55cfc8770b69cc07268fae7f25ee444c.cpt");
        sprintf(decrypt_sn, "ccrypt -d /tmp/fsn-55cfc8770b69cc07268fae7f25ee444c.cpt -K %s", p);
        system(decrypt_sn);
        FILE *file = fopen("/tmp/fsn-55cfc8770b69cc07268fae7f25ee444c", "r");
        if (file == NULL) {
            printf("Error opening file!\n");
            exit(1);
        }
        while (fgets(line, sizeof line, file) != NULL) /* read a line from a file */ {
            //fprintf(stdout, "%s", line); //print the file contents on stdout.
            if (strncmp(line, key, strlen(key)) == 0) {
                char* value = strchr(line, ':');
              if(value!=NULL){/*testing the return value for erros so you prog doesn't cruch*/
                value += 2;
                serial_number = strdup(value);
               }
              /*in case off erreor you can choose one of two options:*/
              /*optinon1: print an error mesage then kill your prog*/
              else{
                printf("Error: corrupted file!\n");
                exit(1);
              } 
              /*option 2: removing the else part your baca then will return NULL and the calling code should understand that an error has occured*/               
                break; 
            }
        }
        fclose(file);
        remove("/tmp/fsn-55cfc8770b69cc07268fae7f25ee444c");
    }
    else {
        printf("fsn not found\n");
    }
    return serial_number;
}
int main(int argc, char* argv[]) {
    char *r;
    char *del;
    char *decrypt;
    int ret;
    char input[30];
    char *p;
    char *original_sn;
    p = "MmI4MTUxM2FjMjRlMDkzYmRkZGQyMjcwMjQ4OWY3MDAwNGZiYTM0MWNkZGIxNTdlYzAxN2";
    //tulis(p);
    original_sn = baca(p);
    if(original_sn!=NULL){
      printf("SN: %s\n", original_sn);
      free(original_sn);/*you should free the memory allocated by strdup once you are done using it.*/
    }
    else{
     printf("An error has occured\n");
    }
    return 0;
}

方法2:通过引用传递

char baca(const char* p, char **serial_number) {
    // Store the serial number from a file in a variable
    char line[50];
    char decrypt_sn[300];
    char key[30] = "Serial Number";
    char ret = 0;/*the return value 0 means no error.*/
    if( access( "/tmp/halo/fsn-55cfc8770b69cc07268fae7f25ee444c", F_OK ) != -1 ) {
        system("cp /tmp/halo/fsn-55cfc8770b69cc07268fae7f25ee444c /tmp/");
        system("mv /tmp/fsn-55cfc8770b69cc07268fae7f25ee444c /tmp/fsn-55cfc8770b69cc07268fae7f25ee444c.cpt");
        sprintf(decrypt_sn, "ccrypt -d /tmp/fsn-55cfc8770b69cc07268fae7f25ee444c.cpt -K %s", p);
        system(decrypt_sn);
        FILE *file = fopen("/tmp/fsn-55cfc8770b69cc07268fae7f25ee444c", "r");
        if (file == NULL) {
            printf("Error opening file!\n");
            exit(1);
        }
        while (fgets(line, sizeof line, file) != NULL) /* read a line from a file */ {
            //fprintf(stdout, "%s", line); //print the file contents on stdout.
            if (strncmp(line, key, strlen(key)) == 0) {
                char* value = strchr(line, ':');
                if(value!=NULL){/*testing the return value for erros so you prog doesn't cruch*/
                value += 2;
                *serial_number = strdup(value);
               }
              /*in case off erreor you can choose one of two options:*/

              else{
                 /*optinon1: print an error mesage then kill your prog*/
                 /*option 2: making the return value non 0 and the calling code should understand that an error has occured*/
                #define OPTION1
                #ifdef  OPTION1
                printf("Error: corrupted file!\n");
                exit(1);
                #else
                ret=-2; //to used this option comment out #define OPTION1
                #endif  
              }            
                break; 
            }
        }
        fclose(file);
        remove("/tmp/fsn-55cfc8770b69cc07268fae7f25ee444c");
    }
    else {
        printf("fsn not found\n");
        ret=-1;
    }
    return ret;
}
int main(int argc, char* argv[]) {
    char *r;
    char *del;
    char *decrypt;
    int ret;
    char input[30];
    char *p;
    char *original_sn=NULL;
    p = "MmI4MTUxM2FjMjRlMDkzYmRkZGQyMjcwMjQ4OWY3MDAwNGZiYTM0MWNkZGIxNTdlYzAxN2";
    //tulis(p);
     switch(baca(p,&original_sn))
     {
        case 0: //evrything is fine
           printf("SN: %s\n", original_sn);
           free(original_sn);
           break;
       case -1:/* handle each error as you should*/
       case -2:
       default:
         printf("An error has occured\n");
     }
    return 0;
}

希望这会有所帮助。 :)