指针值的变化

时间:2014-02-14 11:40:35

标签: c pointers semantics

输入文件:

JOHN 9999999999
MEENA 9888888888
RIA 8718218218
SONIA 7777777777
FINCH 1234567890
  • 我想通过将文件内容附加到list中来创建循环链表。我创建一个带有“NO VALUE”条目的sentinal。

  • 当我在gdb中测试我的代码p-> next->在uploadAddresses()的while循环结束时,name显示了Finch.How是否可能?p值保持不变只有临时值是每次迭代后改变。

    #include<stdio.h>    
    #include<stdlib.h>    
    #include<string.h>    
    #define MAXPHONENO 100
    
    //Variables declaration 
    struct Contact {
      char *name;
      char *mobileno;
      struct Contact *next;
    };
    typedef struct Contact Contact;    
    typedef Contact* PHBOOK;
    
    //Function declaration
    PHBOOK createAddressBook();
    PHBOOK uploadAddresses(PHBOOK pb,char *name);
    
    //Implementation
    PHBOOK createAddressBook(){    
      PHBOOK pb = (PHBOOK)malloc(sizeof(Contact));   
      pb->name = "NO ENTRY";    
      pb->mobileno = "NO ENTRY";    
      pb->next = pb;    
      return pb;    
    }
    
    
    PHBOOK uploadAddresses(PHBOOK p,char *fname){
      PHBOOK temp = p;
      char mob[20],name[20];
      FILE *fp = fopen(fname,"r");
      while(fscanf(fp,"%s %s",name,mob)!=EOF){
        temp->next = (PHBOOK)malloc(sizeof(Contact));    
        temp = temp->next;    
        temp->name = name;    
        temp->mobileno = mob;    
        printf("%s %s\n",name,mob);    
      }    
      temp->next = p;    
      return p;    
    }   
    
    int main(){    
      PHBOOK pb = createAddressBook();    
      pb = uploadAddresses(pb,"/home/user/names_mobile");    
      return 0;
    }
    

4 个答案:

答案 0 :(得分:1)

temp->name = name;

您只将指针指定给temp->name

此指针指向局部变量。

编辑:

在您的情况下,fscanf使用char name [20]来包含输入名称,在每个循环中更改其上下文。在while循环结束时它将是“Finch”。

我们知道“temp-&gt; name”指向包含“Finch”的“char name [20]”,这就是你看到它的原因。

答案 1 :(得分:1)

变化:

temp->name = name;
temp->mobileno = mob;    

要:

temp->name = malloc(strlen(name)+1);
temp->mobileno = malloc(strlen(mob)+1);
strcpy(temp->name,name);
strcpy(temp->mobileno,mob);

答案 2 :(得分:0)

malloc一些内存

char *name;
char *mobileno;

这也将解决您的问题。

PHBOOK uploadAddresses(PHBOOK p,char *fname){

PHBOOK temp = p;
char mob[20],name[20];

FILE *fp = fopen(fname,"r");


  while(fscanf(fp,"%s %s",name,mob)!=EOF)
  {
    temp->next = (PHBOOK)malloc(sizeof(Contact));  
    temp = temp->next; 
    temp->name = (char*)malloc(sizeof(char) * MAX_NAME) ;    
    temp->mobileno = (char*)malloc(sizeof(char) * MAX_PHNO) ;  

   strcpy(temp->name,name);
    strcpy(temp->mobileno,mob);

  }    
    temp->next = p;    
    return p;    

}

答案 3 :(得分:0)

改进代码:

注意&lt;%ms&gt;修饰符,为字符串动态分配空间。还必须关闭文件,否则会发生资源泄漏。

    PHBOOK uploadAddresses(PHBOOK p,char *fname){
      PHBOOK temp = p;
      char *mob,*name;
      FILE *fp = fopen(fname,"r");

      if (!fp)
        return temp;

      while (fscanf(fp,"%ms %ms",&name,&mob)!=EOF) {  
        temp->next = malloc(sizeof(Contact));       
        temp = temp->next;
        temp->name = name;    
        temp->mobileno = mob;    
        printf("%s %s\n",name,mob);    
      }

      fclose(fp);
      temp->next = p;    
      return p;  
    }