警告:赋值在没有强制转换的情况下从指针生成

时间:2014-07-03 07:45:59

标签: c pointers compiler-warnings

我正在学习C差不多一年,这是我第一次收到警告。 这是我的代码:

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


 int socket_creation(FILE* fp){
        int s;
        s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        if (s == INVALID_SOCKET){
                printf("Error occurred while creating socket: %d\n", WSAGetLastError());
                fprintf(fp, "Error occurred while creating socket: %d\n", WSAGetLastError());
        }
        else{
                printf("Socket creation was successful.\n");
                fprintf(fp, "Socket creation was successful.\n");
        }
        return s;
}


void connect_to_server(int s, struct sockaddr_in ClientService, FILE* fp){
        int cResult;
        cResult = connect(s, (struct sockaddr*) &ClientService, sizeof(ClientService));
        if (cResult == SOCKET_ERROR){
                printf("connection to the server has been failed: %d\n", WSAGetLastError());
                fprintf(fp, "connection to the server has been failed: %d\n", WSAGetLastError());
                cResult = closesocket(s);
                if (cResult == SOCKET_ERROR){
                        printf("error occurred while trying to close socket. \n");
                        fprintf(fp, "error occurred while trying to close socket. \n");
                }
                WSACleanup();
        }
        else{
                printf("Connection to serevr has been made successfully. \n");
                fprintf(fp, "Connection to serevr has been made successfully. \n");
        }
}

int send_to_serv(char buffer[], int s){
        int sendto;
        sendto = send(s, buffer, 1024, 0);
        if (sendto == -1)
                printf("\nError: couldn't send the Code.\n", buffer);
        else printf("\nCode: <%s> SENT.\n", buffer);
        return sendto;
}

int recv_from_serv(int s, int* numberLines, FILE *fp){

        int recvfrom;
        char buffer[1024] = "";

        recvfrom = recv(s, buffer, 1024, 0);
        if (recvfrom == -1)
        printf("\nError: couldn't receive Code. !\n");
        else printf("\nRespond: <%s>, RECEIVED. \n", buffer);
        fprintf(fp, "\n");
        fprintf(fp, buffer);

        *numberLines = atoi(buffer + 3);
        return recvfrom;
}

int main() {

        WSADATA info;
        int error, s;
        int sResults, sendError, recvError,convert2;
        char buffer[1024] = "";
        char recvbuf[1024] = "";
        int numberLines, i, temp, convert;
        char converted_num[1024] = "";
        struct sockaddr_in ClientService;
        FILE *fp = fopen("stored_data.txt", "w");
        char* lines_array;
        error = WSAStartup(MAKEWORD(2, 0), &info);

        if (error != 0){
                printf("WSAstartup failed with error: %d\n", error);
                exit(1);
        }

        for (i=0; i <=numberLines; i++) {
                lines_array[i]=NULL;
        }
        s = socket_creation(fp);

        ClientService.sin_family = AF_INET;
        ClientService.sin_addr.s_addr = inet_addr("54.209.143.42");
        ClientService.sin_port = htons(6714);

        connect_to_server(s, ClientService, fp);

        strcpy(buffer, "100");
        sendError = send_to_serv(buffer, s);
        recvError = recv_from_serv(s, &numberLines, fp);

        strcpy(buffer, "400");
        sendError = send_to_serv(buffer, s);
        recvError = recv_from_serv(s, &numberLines, fp);
        printf("\nNumber of Lines are: %d\n", numberLines);

        lines_array = malloc(sizeof(char*)*numberLines);
        temp = numberLines;

        for (i = 0; i < temp; i++){
                convert = 5000001 + i;
                _itoa(convert, converted_num, 10);
                sendError = send_to_serv(converted_num, s);
                convert2=atoi(convert);
                convert=convert%10000;
                if(convert2==0) {
                        for(i=0; i<=1024; i++) {
                                buffer[i]=0;
                        }
                        lines_array= (convert);
                        recv_from_serv(s, &numberLines, fp);
                }
                else{
                        for(i=0; i<=1024; i++) {
                                buffer[i]=0;
                        }
                }
        }

        close(fp);
        //system("PkAUSE>nul");
        system("PAUSE");
        return 0;
 }

这是我的编译器(MinGW)给我的输出:

  

finalproject1.c:在函数main': finalproject1.c:87: warning: assignment makes integer from pointer without a cast finalproject1.c:113: warning: passing arg 1 of atoi'中生成指针   来自整数而没有强制转换finalproject1.c:119:警告:赋值   从整数生成指针而不使用强制转换finalproject1.c:133:3:   警告:文件末尾没有换行符

请帮助我,我不知道该怎么办,当我请他帮助我的时候,我的指导告诉我“Google it”。

2 个答案:

答案 0 :(得分:1)

触发警告的行是:

lines_array[i]=NULL;

并且这个变量声明如下:

char* lines_array;

所以你将一个指针(NULL宏扩展为系统上的指针类型值)塞进一个char,它不会适合并且是一个这是非常奇怪的事情。

可能你想要一个字符指针数组,即

char **lines_array;

但是一旦你知道你将要处理多少行,你当然必须分配数组。

答案 1 :(得分:0)

首先,我可以看到你在for循环中使用整数 numberLines 作为条件而不首先初始化它:

int numberLines;
for (i=0; i <=numberLines; i++) {
            lines_array[i]=NULL;
    }

如果您想将数组初始化为null,则可以将其初始化为:

char* lines_array = null;

试试这个并告诉我警告是否仍然存在。