讨论/题目交流/求助“739. 每日温度”编译总是失败/
求助“739. 每日温度”编译总是失败

简介

代码原文:

#include "string.h"

typedef struct tag_temperStack {
    int index; /* 表示温度在temperatures数组中的位置 */
    int val; /* 表示温度 */
    struct tag_temperStack *pNext;
} temperatureStack;

int* dailyTemperatures(int* T, int TSize, int* returnSize){
    if (T == NULL || TSize <= 0 || returnSize == NULL) {
        return NULL;
    }

    int loop = 1;
    temperatureStack *tempStack; /* 用以表示栈顶 */
    temperatureStack *tempStack2;
    int *outputArr = NULL;

    temperatureStack *stack = (temperatureStack *)malloc(sizeof(temperatureStack));
    if (stack == NULL) {
        return NULL;
    }
    (void)memset_s(stack, 0, sizeof(temperatureStack));

    stack->index = 0;
    stack->val = T[0];
    stack->pNext = NULL;

    tempStack = stack;

    /* 用以返回结果的数组 */
    outputArr = (int *)malloc(TSize * sizeof(int));
    if (outputArr == NULL) {
        free(stack);
        return NULL;
    }
    (void)memset_s(outputArr, 0, TSize * sizeof(int));

    while (loop < TSize) {
        /* 当数组元素大于栈顶元素,弹出栈顶,将新的数组元素放入栈顶,并且将结果数组赋值 */
        if (T[loop] > tempStack->val) {
            outputArr[tempStack->index] = loop - tempStack->index;
            tempStack->index = loop;
            tempStack->val = T[loop];
        } else {
            /* 当数组元素小于栈顶元素,将新的元素盖到栈顶上 */
            tempStack2 = (temperatureStack *)malloc(sizeof(temperatureStack));
            if (tempStack2 == NULL) {
                break;
            }

            (void)memset_s(tempStack2, 0, sizeof(temperatureStack));
            tempStack2->index = loop;
            tempStack2->val = T[loop];
            tempStack2->pNext = NULL;
            
            tempStack->pNext = tempStack2;
            tempStack = tempStack2;
        }
        loop++;
    }

    /* 栈内剩余元素全部释放 */
    while(stack != NULL) {
        tempStack = stack->pNext;
        free(stack);
        stack = tempStack;
    }

    *returnSize = TSize;
    return outputArr;
}

编译报错结果:

solution.c: In function 'main'
Line 35: Char 20: warning: implicit declaration of function 'dailyTemperatures' [-Wimplicit-function-declaration]
                    ^
Line 35: Char 20: warning: initialization of 'int *' from 'int' makes pointer from integer without a cast [-Wint-conversion]
/tmp/ccwJkFxY.o: In function `main':
At .text+0x324: undefined reference to `dailyTemperatures' (prog_joined.c)
collect2: error: ld returned 1 exit status

不太懂,int* dailyTemperatures(int* T, int TSize, int* returnSize)这个函数不是官方提供的吗,为啥会出现找不到声明的地方?

展开讨论
关你屁事发起于 2020-02-29
最近编辑于 2020-02-29

你别用string.h的memset_s直接用memset。是string.h导致的异常,具体是甚么原因我不是很清楚。