目標:在Linux機器上打印當前工作目錄。打印使用指針在c中顯示工作目錄
不使用指針,它給正確的輸出..
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<errno.h>
int main()
{
//char buf[1024];
char * buf;
char * cwd;
buf = (char *)malloc(sizeof(char) * 1024);
if((cwd = getcwd(buf, sizeof(buf))) != NULL)
printf("pwd : %s\n", cwd);
else
perror("getcwd() error : ");
return 0;
}
但隨着指針它顯示了以下錯誤
getcwd() error : : Numerical result out of range