gpt4 book ai didi

c - 指针未给出正确的值

转载 作者:行者123 更新时间:2023-11-30 15:12:10 25 4
gpt4 key购买 nike

我正在为学校做一个项目,并试图学习如何使用指针。我试图让他们传递 X、Y 和 Z 的值。但是,我总是得到错误的值。 X 应该等于 1。Y 应该等于 2。Z 应该等于 3。

{
// Add the C statement(s) necessary to accomplish the task identified in the comments below


double A[3] = {2.718, 3.14, 2.718}; // declare an array A of 3 doubles

A[1] = 3.14; // put the value 3.14 in the middle location of A

A[0] = 2.718;
A[2] = 2.718; // put the value 2.718 in the first and last locations of A


printf("The first value of A is %f\n",A[0]); // print the first value in A


printf("The last value of A is %f\n",A[2]); // print the last value in A


A[2] = A[0] + A[1]; // change the last value in A so that it is the sum of the first two and then print it


printf("The last value of A is %f\n", A[2]);
int B[4] = {10, 25, 50, 100}; // declare an array B of 4 integers with initial values 10, 25, 50, 100


int sum = B[0] + B[1] + B[2] + B[3];
printf("The sum of all four elements in B is %d\n",sum); // print the sum of all four elements in B


printf("Four elements in B in reverse order %d %d %d %d\n",B[3], B[2], B[1], B[0]); // print the four elements in B in reverse order (100, 50, 25, 10)


int X = 1;
int Y = 2;
int Z = 3;
printf("%d, %d, %d\n,",X,Y,Z); // declare three integers, X Y and Z, assign them the values 1, 2 and 3 and print them


int *P1; // declare three pointers to integers, P1, P2 and P3


int *P2;
int *P3;
P1 = &X;
P2 = &Y;
P3 = &Z; // point P1 to X, P2 to Y, and P3 to Z.


printf("%d, %d, %d\n",P1,P2,P3); // print the values at X and Y and Z using the pointers P1 to P3
*P1 = 10;
printf("%d", P1); // using P1 and not X, change the variable X's value from 1 to 10, then print it


return 0;
}

最佳答案

&XX的地址设置为P1。但是,如果您想访问存储在该地址的值,那么您应该*P1

*P1 是什么意思?表示该地址存储的值。

P1=&X; //P1 takes address value of X;

如果您想在该地址打印值,那么

printf("Value of X %d\n",*P1);

关于c - 指针未给出正确的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35204866/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com