更改结构中元素的值

发布于 2025-01-13 17:10:37 字数 583 浏览 3 评论 0原文

我是结构新手。我正在尝试编写一个具有结构的程序,该结构应该存储字符数组及其长度。我希望能够更改长度的值,因为我将创建诸如修剪/连接数组之类的函数。这是我编写的代码:

#include <stdio.h>
#include <stdlib.h>
struct strstruct{
unsigned int length;
char string[20];
};
typedef struct strstruct stru;
int strleng(stru A){
  int i=0;
  while(A.string[i]!='\0'){
    i++;
  }
  A.length =i;
  return i;
}
int main(){

  stru A = {1,
 {'a','b','c','d','e','f'}
  };
  
  printf("%d %d\n",strleng(A),A.length);
  return 0;
}

尽管调用了 strleng,A.length 的值并没有改变。
(一)为什么?
(ii) 还有其他方法吗?

I'm new to structs. I am trying to write a program that has a struct, and the struct is supposed to store a character array and its length. I want to be able change the length's value as I would be creating functions like trimming/concatenating the array. Here is a code I wrote:

#include <stdio.h>
#include <stdlib.h>
struct strstruct{
unsigned int length;
char string[20];
};
typedef struct strstruct stru;
int strleng(stru A){
  int i=0;
  while(A.string[i]!='\0'){
    i++;
  }
  A.length =i;
  return i;
}
int main(){

  stru A = {1,
 {'a','b','c','d','e','f'}
  };
  
  printf("%d %d\n",strleng(A),A.length);
  return 0;
}

The value of A.length is not changing inspite of calling strleng.
(i)Why?
(ii) Is there another way to do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小女人ら 2025-01-20 17:10:37

对于初学者来说,函数调用中参数的求值顺序是未指定的。

因此,在此调用中,

 printf("%d %d\n",strleng(A),A.length);

参数表达式 A.length 的计算可以发生在调用函数 strleng 之前,反之亦然。

其次,像这样声明的函数strleng

int strleng(stru A);

处理在main中声明并用作参数的原始对象A的副本。因此更改副本不会影响原始对象。

您需要通过指向该对象的指针通过引用传递该对象。

unsigned int strleng( stru *A){
  unsigned int i=0;
  while(A->string[i]!='\0'){
    i++;
  }
  A->length =i;
  return i;
}

在 main 中,您应该编写例如,

unsigned int n = strleng( &A );
printf("%u %u\n", n, A.length );

请注意,一方面,数据成员 length 被声明为具有 unsigned int 类型,

unsigned int length;

另一方面,在您的原始文件中函数 strleng 您正在使用有符号类型 int 的对象,并且函数返回类型也是 int。该函数应至少使用相同的类型 unsigned int 而不是类型 int

For starters the order of evaluation of arguments in a function call is unspecified.

So in this call

 printf("%d %d\n",strleng(A),A.length);

the evaluation of the argument expression A.length can occur before calling the function strleng or vice versa.

Secondly the function strleng declared like

int strleng(stru A);

deals with a copy of the original object A declared in main and used as an argument. So changing the copy does not influence on the original object.

You need to pass the object by reference through a pointer to it.

unsigned int strleng( stru *A){
  unsigned int i=0;
  while(A->string[i]!='\0'){
    i++;
  }
  A->length =i;
  return i;
}

and in main you should write for example

unsigned int n = strleng( &A );
printf("%u %u\n", n, A.length );

Pay attention to that on one hand, the data member length is declared as having the type unsigned int

unsigned int length;

On the other hand, within your original function strleng you are using an object of the signed type int and the function return type is also int. The function should use at least the same type unsigned int instead of the type int.

第几種人 2025-01-20 17:10:37

尝试下面的代码:

#include <stdio.h>
#include <stdlib.h>
struct strstruct{
unsigned int length;
char string[20];
};
typedef struct strstruct stru;
int strleng(stru* A){
  int i=0;
  while(A->string[i]!='\0'){
    i++;
  }
  A->length =i;
  return i;
}
int main(){

  stru A = {1,
 {'a','b','c','d','e','f'}
  };
  
  printf("%d %d %d\n",A.length, strleng(&A),A.length);
  printf("%d \n",A.length);
  return 0;
}

您将得到输出:6 6 1。我现在应该得到答案了。

如果要修改函数内部结构体的值,首先需要使用指针作为参数。

对于您的问题:

  • 对于大多数 C 编译器来说,printf 函数内部的函数是从右向左处理的。我认为你的情况下的编译器就是这个。
  • 对于某些 C 编译器,它从左到右在一行中处理函数。

希望能帮到你,c在线编译器:https://www.onlinegdb.com/online_c_compiler

Try the code below:

#include <stdio.h>
#include <stdlib.h>
struct strstruct{
unsigned int length;
char string[20];
};
typedef struct strstruct stru;
int strleng(stru* A){
  int i=0;
  while(A->string[i]!='\0'){
    i++;
  }
  A->length =i;
  return i;
}
int main(){

  stru A = {1,
 {'a','b','c','d','e','f'}
  };
  
  printf("%d %d %d\n",A.length, strleng(&A),A.length);
  printf("%d \n",A.length);
  return 0;
}

You will get output: 6 6 1. I should get the answer now.

At first, you need to use pointer as a parameter if you want to modify struture's value inner a fucntion.

For your question:

  • To most of the c compiler, the functions inner a printf function is prcessed from right to left. I think the compiler in your case is this one.
  • For some c compiler, it do process functions in one line from left to right.

I hope it can help you, c online compiler: https://www.onlinegdb.com/online_c_compiler.

私野 2025-01-20 17:10:37
printf("%d %d\n",strleng(A),A.length);
  1. 首先,这里将参数作为值传递给 strleng 函数意味着 strleng 函数的参数是 A 的副本。换句话说,主函数中的变量 A 和 strleng 函数内的结构体变量是两个自变量。因此,在 strleng 函数中更改 A.length 对于主函数中的变量 A 来说是不可见的。 (有许多关于按值传递与按引用传递的优秀在线资源。您可以查看这些资源以更好地理解)
  2. 大多数编译器从从右到左获取 printf() 的每个参数。所以这里先执行A.length,然后执行strleng(A)。因此,即使您通过引用传递参数,它仍然会输出 6 1。

更新的代码

#include <stdio.h>
#include <stdlib.h>
struct strstruct {
    unsigned int length;
    char string[20];
};
typedef struct strstruct stru;
int strleng(stru* A) {
    int i = 0;
    while(A->string[i] != '\0'){
    i++;
    }
    A->length = i;
    return i;
}
int main() {

    stru A = {1, {'a','b','c','d','e','f'}};

    printf("%d %d %d\n", A.length, strleng(&A), A.length);//6 6 1
    return 0;
}
printf("%d %d\n",strleng(A),A.length);
  1. Firstly, Here you are passing the argument to the strleng function as a value means strleng function' parameter is a copy of A. In other words, variable A in the main function and structure variable inside the strleng function are two independent variables. So changing A.length in the strleng function will not be visible to your variable A in the main function. (There are many good online resources available about Pass by value vs. Pass by reference. You can check those for better understanding)
  2. Most of the compilers takes each parameter of printf() from right to left. So here A.length execute first then strleng(A). So even you pass the argument by reference, it will still output 6 1.

Updated Code

#include <stdio.h>
#include <stdlib.h>
struct strstruct {
    unsigned int length;
    char string[20];
};
typedef struct strstruct stru;
int strleng(stru* A) {
    int i = 0;
    while(A->string[i] != '\0'){
    i++;
    }
    A->length = i;
    return i;
}
int main() {

    stru A = {1, {'a','b','c','d','e','f'}};

    printf("%d %d %d\n", A.length, strleng(&A), A.length);//6 6 1
    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文