关于sizeof( )函数的问题
程序代码:
#include <stdio.h> #include <stdlib.h> #include <string.h> #define _STD_WANT_LIB_EXT1_ 1 #define Count 5 #define buf_len 100 int main(void) { char buf[buf_len] = {'0'}; // input buffer int str_count = 0; // string 1st count = 0 int str_len = 0; // string current length int outp_str_len = 0; // output string length int str_max = Count; // max count input string char** ptemp = NULL; char** ps = (char**)calloc(str_max, sizeof(char*));//5 * char* = 40 byte do { printf("input string you want (less than %d wards):", buf_len); fflush(stdin); char* ptest = (char*)gets_s(buf, sizeof(buf)); if(ptest == NULL) { printf("gets_s() error\n"); free(ps); ps = NULL; //free(ptest); //ptest = NULL; return 1; } //printf("%s\n", buf); else if(buf[0] == '0') { printf("You input nothing!!!\n"); break; } else if(str_count == str_max) { str_max += str_count/4; //add 25% merry ptemp = realloc(ps, str_max); if(ptemp == NULL) { printf("realloc() error\n"); return 2; } ps = ptemp; } str_len = strnlen_s(buf, sizeof(buf)) + 1;// buf current length must <= buf_len ps[str_count] = malloc(str_len); //allocation current merry to ps[...] if(ps[str_count] == NULL) { printf("error malloc() error!!!\n"); return 3; } strcpy_s(ps[str_count++] ,str_len, buf); printf("Do you want to input another string? (Y/N): "); fflush(stdin); } while(tolower((char)getchar()) == 'y'); printf("%s\n", ps[str_count-1]); //string output success outp_str_len = strnlen_s(ps[str_count - 1], sizeof(ps[str_count - 1]));//some errors!! sizeof() function if(outp_str_len == 0) { printf("strnlen() error!!!\n"); return 4; } printf("outp_str_len = %d\n", outp_str_len); //length of string wrong //printf("sizeof(char*) = %zd\n", sizeof(char*)); return 0; //这个程序可以输入100个字符以内的任意个数的字符串,我想用它给输入的字符串进行长度排序,程序没写完,在检查字符串长度时出错了。 //我觉得是sizeof()的问题。。。。 }