我目前在C中存在鏈接列表和指針問題。我遇到的問題是將數據添加到鏈接列表中。目前,我有:鏈接列表/指針的問題
struct str_pair{
char ip [50] ;
char uri [50] ;
struct str_pair *next ;
};
struct str_pair *it ;
struct str_pair *header = NULL; // Start of linked list
struct str_pair *ptr; // Moves along the list
struct str_pair *ptr2; // Another pointer
struct str_pair *ptr3;
void addData(char *addURI, char *addIP){
struct str_pair *tmp, *tmp2;
tmp = (str_pair*)malloc(sizeof(str_pair)); // Create new space in str_pair
strncpy(tmp->uri, addURI, 49);
strncpy(tmp->ip, addIP, 49);
tmp->next = NULL;
if (header == NULL) { header = tmp; }
else
{
tmp2 = header;
while (tmp2->next != NULL) { tmp2 = tmp2->next; }
tmp2->next = tmp;
}
}
我想要做的是通過一個URL和IP地址,通過它應該加上這些值到鏈表中的參數。
這裏是代碼調用這個函數:
int main(int argc, char *argv[])
{
int incrItems=0;
int j;
header = NULL;
for(j = 1; j < argc; j++)
{
char ch=argv[j][0];
switch(ch)
{
case 'A' :
{
char *newURI = argv[j+1];
char *newIP = argv[j+2];
incrItems++;
addData(newURI,newIP);
j=j+2;
break;
}
*Snipped the rest as its unnecessary*
我遇到的問題是,傳遞的參數沒有被添加到鏈表。編譯時不顯示錯誤。
你到底有什麼問題? – Mat
你竟忘了提你的問題! –
問題是它沒有工作。來自傳遞參數的數據不會顯示在鏈接列表中。編譯代碼時不顯示錯誤。 – George