小编you*_*aza的帖子

使用类型转换的分段错误

当我运行以下代码时,它会给出一个分段错误:

#include <stdio.h>  

int main() {  
  int i;  
  char char_array[5] = {'a', 'b', 'c', 'd', 'e'};  
  int int_array[5] = {1, 2, 3, 4, 5};  
  unsigned int hacky_nonpointer;  
  hacky_nonpointer = (unsigned int) char_array;  
  for(i=0; i < 5; i++) { // Iterate through the int array with the int_pointer.  
    printf("[hacky_nonpointer] points to %p, which contains the char '%c'\n",  
    hacky_nonpointer, *((char *) hacky_nonpointer));  
    hacky_nonpointer = hacky_nonpointer + sizeof(char);  
  }  
  hacky_nonpointer = (unsigned int) int_array;  
  for(i=0; i < 5; i++) { // Iterate through …
Run Code Online (Sandbox Code Playgroud)

c memory

-3
推荐指数
1
解决办法
82
查看次数

标签 统计

c ×1

memory ×1