- 论坛徽章:
- 0
|
下面一個小程序是我編寫的程序中抽取出來的,我覺得能夠通過,但結果卻segmentation fault, 鏈表創建函數的返回值如果改成指針型的LNode*,可以輸出預期結果,但如果void型就不行。但我覺得參數裏已經傳了可以在參數中自動返回回去的,望大家指教下面程序爲什麽會段錯誤?
#include <stdio.h>
#include <stdlib.h>
typedef struct LNode
{
int iData;
struct LNode *pNext;
} LNode;
void CreateLink(LNode *L, int iCount)
{
int iNum;
LNode *pointer = NULL;
LNode *pNew = NULL;
L = (LNode*)malloc(sizeof(LNode));
L -> pNext = NULL;
pointer = L;
printf("The create serial is:\n");
for (iNum = 0; iNum < iCount; ++iNum)
{
pNew = (LNode *)malloc(sizeof(LNode));
if (NULL == pNew)
{
printf("Cannot distribute enough room!\n");
return ;
}
scanf("%d", &pNew->iData);
pNew->pNext = NULL;
pointer->pNext = pNew;
pointer = pNew;
pNew = NULL;
}
return ;
}
int main(void)
{
LNode *L = NULL;
LNode *pointer = NULL;
int iNum;
printf("How many nodes you want to create?\n");
scanf("%d",&iNum);
CreateLink(L, iNum);
pointer = L;
printf("The serial of LinkList has been created is:\n");
while (NULL != pointer->pNext)
{
pointer = pointer->pNext;
printf("%d\n", pointer->iData);
}
return 0;
} |
|