简体   繁体   English

我正在学习C语言中的指针,并且从一个基本概念开始,但是我一直在出错。 我究竟做错了什么?

[英]I am learning about pointers in C and am starting with a basic idea, but I keep getting errors. What am I doing wrong?

I am learning about pointers in C and am starting with a basic idea: using a pointer to go through and change an array. 我正在学习C语言中的指针,并从一个基本概念开始:使用指针来遍历和更改数组。 Then print that array. 然后打印该数组。 What mistakes am I making? 我犯什么错误?

I was told you can go through an array by assigning it to a pointer variable, as I have done below: 有人告诉我可以通过将数组分配给指针变量来遍历数组,如下所示:

int main(void){
  int i = 0;
  char entry[40], n, p;

  p = entry;

  printf("Enter a sentence: ");
  while ((n = getchar()) != '\n'){
    *(p + i) = n;
    i++;
  }
  *(p + i) = 0;

  printf("%s", p);

  return 0;
}

I've been experimenting with different ways of doing this for a bit now and could use some help. 我已经尝试了一些不同的方法来进行此操作,并且可以使用一些帮助。 I keep receiving errors such as: 我不断收到如下错误:

  • assignment makes integer from pointer without a cast 赋值使指针从整数变为整数

  • invalid lvalue in assignment 分配中无效的左值

  • assignment makes integer from pointer without a cast 赋值使指针从整数变为整数

    This refers to code p = entry where you try to assign a pointer ( entry ) to p which is of type char 这是指代码p = entry ,您尝试在其中分配给char类型为p的指针( entry

  • invalid lvalue in assignment 分配中无效的左值

    This refers to *(p + i) = n as (p + i) is a number (a char can be considered as an int for this case) and not a pointer, thus cannot be assigned. 这是指*(p + i) = n因为(p + i)是一个数字(在这种情况下,可以将char视为int)而不是指针,因此无法分配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM