You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

字符串内指针移动问题:解读代码中buffer内指针的移动逻辑

Understanding Pointer Movement in Your C Code

Let's walk through exactly how the pointers shift and interact in your code—strtok is especially tricky because it relies on internal state, so tracing each line step by step will help clarify what's happening. First, let's fill in the obvious missing parts of your snippet to make the context clear:

#define buf_size 256
FILE *fp = fopen("data.txt", "r");
char buffer[buf_size];
char *read_line_p;
char *string_field_in_read_line_p;
char *integer_field_in_read_line_p;
char *string_field_1;
char *string_field_2;

while(fgets(buffer, buf_size, fp) != NULL){ 
    read_line_p = malloc((strlen(buffer)+1)*sizeof(char)); 
    strcpy(read_line_p,buffer); 
    string_field_in_read_line_p = strtok(read_line_p,","); 
    integer_field_in_read_line_p = strtok(NULL,","); 
    string_field_1 = malloc((strlen(string_field_in_read_line_p)+1)*sizeof(char)); 
    string_field_2 = malloc((strlen(string_field_in_read_line_p)+1)*sizeof(char));
    strcpy(string_field_1, string_field_in_read_line_p);
    strcpy(string_field_2, string_field_in_read_line_p);
    // ... rest of your code (freeing memory, processing fields, etc.)
}

Now let's break down the pointer movement line by line:

1. fgets(buffer, buf_size, fp)

  • buffer is a fixed-size character array—its name acts as a pointer to the first character in the array, and this pointer never moves.
  • fgets reads a line from the file into buffer, stopping at a newline, end of file, or when it fills buf_size-1 characters. It adds a null terminator \0 at the end of the string.
  • Only the content inside buffer changes here; the pointer itself stays anchored to the start of the array.

2. read_line_p = malloc((strlen(buffer)+1)*sizeof(char))

  • malloc allocates a new, separate block of memory big enough to hold the string in buffer (plus space for the null terminator).
  • read_line_p now points directly to the start of this fresh memory block.

3. strcpy(read_line_p, buffer)

  • strcpy copies every character from buffer (starting at its first character) into the memory block pointed to by read_line_p, including the null terminator.
  • read_line_p remains pointed to the start of its memory block. The two pointers (buffer and read_line_p) reference completely separate memory—changes to one won't affect the other.

4. string_field_in_read_line_p = strtok(read_line_p, ",")

This is where the critical pointer manipulation happens with strtok:

  • strtok scans the string starting at read_line_p until it finds the first , delimiter.
  • It replaces that , with a null terminator \0 (this modifies the content in read_line_p's memory block!).
  • It returns a pointer to the start of the first field (the text before the comma)—this is what string_field_in_read_line_p now points to.
  • Behind the scenes, strtok stores the address of the character immediately after the replaced comma in an internal static variable. This is how it remembers where to resume scanning next time.

5. integer_field_in_read_line_p = strtok(NULL, ",")

  • Passing NULL to strtok tells it to use the internal saved address from the last call to start scanning.
  • It finds the next , (or the end of the string) and replaces it with \0 (again modifying read_line_p's memory).
  • It returns a pointer to the start of the second field (the text after the first comma)—this is what integer_field_in_read_line_p points to.

6. string_field_1 = malloc(...) and string_field_2 = malloc(...)

  • Each malloc creates a new memory block sized to hold the first field (from string_field_in_read_line_p).
  • string_field_1 and string_field_2 each point to the start of their own new memory blocks. When you strcpy the field into them, these pointers stay fixed at the start of their blocks—only the content inside changes.

Key Takeaways

  • buffer is a fixed array pointer—it never moves, only its content is updated by fgets.
  • read_line_p, string_field_1, and string_field_2 are pointers to dynamically allocated memory; they stay pointed to the start of their blocks unless you explicitly reassign them.
  • strtok modifies the original string it's given (in read_line_p's memory) and uses internal state to track its position between calls—this is why you shouldn't use strtok on read-only strings (like literals) or in multi-threaded code.

内容的提问来源于stack exchange,提问作者Zeno Raiser

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 06:44:38