简体   繁体   中英

How can i access a array buffer through a pointer which is a member of a structure?

I want to access the arr[8] elements through the pointer inside the structure. I did that code but it is showing error. please help me. i have attached the code below.

#include <stdio.h>

typedef struct
{
  int var;
   int *ptr;
} Info;

int arr[8] = { 1, 3, 5, 7, 9, 8, 6, 4 };
Info x = { 10, &arr[8] };

Info *new_ptr = &x;

void func (Info *new_ptr)
{
   printf ("%d", *(new_ptr->ptr[2]));/* compiler showing error in this line */
 }

int main ()
{
  func (&x);

}
#include <stdio.h>

typedef struct
{
   int var;
   int *ptr;
} Info;

int arr[8] = { 1, 3, 5, 7, 9, 8, 6, 4 };
Info x = { 10, arr };  

void func (Info *new_ptr)
{
   printf ("%d", new_ptr->ptr[2]);
}

int main ()
{
   func (&x);
}

you need to learn some basics about the pointers and arrays.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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