简体   繁体   English

`printf("%d\n",(a->ptr)[i]);` 中的 i 有什么用?

[英]What is the use of i in `printf("%d\n",(a->ptr)[i]);`?

Hey guys I am currently learning DSA and in ADT I have a problem: This is a code that creates a custom array and takes the input of the array and stores it and prints it too but I want to ask that what does that [i] do in printf("%d\n",(a->ptr)[i]);嘿伙计们,我目前正在学习 DSA,在 ADT 中我有一个问题:这是一个创建自定义数组并获取数组的输入并存储它并打印它的代码,但我想问那是什么[i]printf("%d\n",(a->ptr)[i]); that thing is what I am not getting in this code那是我在这段代码中没有得到的东西

#include<stdio.h>
#include<stdlib.h>

struct myArray{
    int total_size;
    int used_size;
    int *ptr;
};

void createArray(struct myArray * a,int tSize,int uSize)
{
    a->total_size = tSize;
    a->used_size = uSize;
    a->ptr = (int *) malloc(tSize * sizeof(int));
}

void show(struct myArray * a){
    for(int i=0; i < a->used_size; i++){
        printf("%d\n",(a->ptr)[i]);
    }
}

void setVal(struct myArray * a){
    int n;
    for(int i=0; i < a->used_size; i++){
        printf("Enter Element %d: ", i);
        scanf("%d",&n);
        (a->ptr)[i] = n;
    }
}

int main(){
    struct myArray marks;
    createArray(&marks,10,2);
    printf("We are running setVal now\n");
    setVal(&marks);

    printf("We are running show now\n");
    show(&marks);
    return 0;
}

The data member ptr points to a dynamically allocated array数据成员 ptr 指向一个动态分配的数组

a->ptr = (int *) malloc(tSize * sizeof(int));

To access elements of the array you can use the subscript operator要访问数组的元素,您可以使用下标运算符

 printf("%d\n",(a->ptr)[i]);

To make it more clear consider the following code snippet.为了更清楚,请考虑以下代码片段。

enum { N = 10 };
int *ptr = malloc( N * sizeof( int ) );
for ( int i = 0; i < N; i++ )
{
    ptr[i] = i;
}

The difference with the original code is the pointer ptr is a data member of a structure and to access ptr using a pointer to an object of the structure type you have to change in the code above the expression与原始代码的区别在于指针 ptr 是结构的数据成员,并且要使用指向结构类型的 object 的指针访问 ptr,您必须在表达式上方的代码中进行更改

ptr[i]

to

( a->ptr )[i]

that is the same as那是一样的

a->ptr[i]

because there are used the postfix operator -> and [] that evaluates left to right.因为使用了从左到右计算的后缀运算符 -> 和 []。

暂无
暂无

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

相关问题 printf(“%d\\n”,scanf(“%d”,&amp;i)) 的输出是什么? - What is the output of: printf(“%d\n”,scanf(“%d”,&i))? printf(“%d”,i ++)和i ++有什么区别? 的printf(“%d”,i)的? - What's the difference between printf(“%d”,i++) and i++; printf(“%d”,i)? C中的%i,%d和%D有什么区别? (printf) - What's the difference between %i, %d and %D in C ? (printf) 谁能解释这一点并交换“ printf(”n=%**d\n“, n, l);” 并解释它是如何工作的?我尝试一下并打印 *d=n - Can anyone explain this and also interchange “ printf(”n=%**d\n“, n, l);” and explain how it works?i try it and print *d=n 类似于“for(i = 1; i &lt;= 10; printf(”%d \\ n“; i),i ++)在C中有效且无UB吗? - Is something like “for(i=1;i<=10;printf(”%d\n";i),i++) valid and UB-free in C? `int main(i){ printf(“i = %d\n”, i); }` 我的值为 1。为什么? 请解释? - `int main(i){ printf(“i = %d\n”, i); }` i value is 1. why ? please explain? printf(“%c”,*(*(ptr + i)+ x))和printf(“%s”,*(*(ptr + i)+ x))之间的差异 - Difference between printf(“%c”,*(*(ptr+i)+x)) and printf(“%s”,*(*(ptr+i)+x)) printf(&quot;%d&quot;) 不显示我输入的内容 - printf("%d") doesn't display what I input 在sscanf(s,“%d%n”,&i,&n)中,n代表什么? - What does the n stand for in `sscanf(s, “%d %n”, &i, &n)`? printf(&quot;%d&quot;,&quot;%u&quot;,&quot;%p&quot;,ptr,ptr,ptr) 和 printf(&quot;%d %u %p&quot;,ptr,ptr,ptr) 有什么区别? - Whats the difference between printf("%d","%u","%p",ptr,ptr,ptr) and printf("%d %u %p",ptr,ptr,ptr)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM