logo
down
shadow

Structures and pointer to pointer element


Structures and pointer to pointer element

By : Nicolas Leborgne
Date : September 23 2020, 09:00 PM
should help you out I assume you are using the "pointer to pointer element" player_t **players to store an array of pointers to multiple players here?
In which case essentially the issue is given ddg->players->ac it doesn't know which player you want, there is an extra level of indirection there. e.g. the first player will be ddg->players[0]->ac and the second player ddg->players[1]->ac.
code :
ddg->players = malloc(sizeof(player_t*) * 2)
ddg->players[0] = amy;
ddg->players[1] = bill;
printf("%d %d", ddg->players[0]->ac, ddg->players[1]->ac);
ddg->players = malloc(sizeof(player_t) * 3);
ddg->players[0].ac = 14;
ddg->players[1].ac = 18;
ddg->players[2].ac = 20;


Share : facebook icon twitter icon
How do I marshall a pointer to a pointer of an array of structures?

How do I marshall a pointer to a pointer of an array of structures?


By : Esh
Date : March 29 2020, 07:55 AM
should help you out Okay, it seems as though your native library does the allocation, so really all you need to do is provide a pointer through which you can access the allocated data.
Change your API definition to (note, I changed the maxData param to uint, long is 64 bits in .NET and 32 bits in native.
code :
[DllImportAttribute("myData.dll", EntryPoint = "myData")]
public static extern int myData(uint myHandle, [MarshalAs(UnmanagedType.LPTStr)] string dataName, out uint Time, out uint maxData, out IntPtr pData);
uint nAllocs = 0, time = 0;
IntPtr pAllocs = IntPtr.Zero;
myData(1, "description", out time, out nAllocs, out pAllocs);
[StructLayoutAttribute(LayoutKind.Sequential, Pack = 1)]
public struct DATASTRUCT
{
    public byte Rel;
    public long Time;
    public byte Validated;
    public IntPtr Data; //pointer to unmanaged string.
}


int szStruct = Marshal.SizeOf(typeof(DATASTRUCT));
DATASTRUCT[] localStructs = new DATASTRUCT[nAllocs];
for(uint i = 0; i < nallocs; i++)
    localStructs[i] = (DATASTRUCT)Marshal.PtrToStructure(new IntPtr(pAllocs.ToInt32() + (szStruct * i)), typeof(DATASTRUCT));
Is it correct to cast a pointer to pointer to pointer to int to a pointer to pointer to pointer to double?

Is it correct to cast a pointer to pointer to pointer to int to a pointer to pointer to pointer to double?


By : Leonardo Rocha
Date : March 29 2020, 07:55 AM
I wish this help you I have a function which deallocates a cubic matrix of double: , Unfortunately not; per 6.2.5p28:
Working with array of structures in C using pointer to pointer

Working with array of structures in C using pointer to pointer


By : Gabriel Adam
Date : March 29 2020, 07:55 AM
will help you Assuming that you want tests to be an array-of-pointers-to-struct-TestCase with 5 pointers to 5 structs, then you need to
allocate an array of 5 pointers, and initialize each pointer by allocating memory for the struct
code :
#include <stdio.h>
#include <stdlib.h>

struct TestCase
{
    int id;
};

int main( void )
{
    int i;
    struct TestCase **tests;

    // allocate memory for an array of 5 pointers
    tests = malloc( 5 * sizeof(struct TestCase *) );
    if ( tests == NULL )
        exit( 1 );

    // allocate memory for 5 structures (and save each pointer in the array)
    for ( i = 0; i < 5; i++ )
    {
        tests[i] = malloc( sizeof(struct TestCase) );
        if ( tests[i] == NULL )
            exit( 1 );
    }

    // initialize the id in each struct
    for ( i = 0; i < 5; i++ )
        tests[i]->id = i;

    // print the id in each struct
    for ( i = 0; i < 5; i++ )
        printf( "%d\n", tests[i]->id );

    // free memory
    for ( i = 0; i < 5; i++ )
        free( tests[i] );
    free( tests );

    return 0;
}
printf( "%p\n", tests );
for ( i = 0; i < 5; i++ )
{
    intptr_t addr = (intptr_t)tests;
    addr += sizeof(struct TestCase)*i;
    addr += (int)&(((struct TestCase*)NULL)->id);

    printf( "%p\n", (void *)addr );
}
0xc03950
0xc03950
0xc03954
0xc03958
0xc0395c
0xc03960
How can I use a pointer to a pointer to a string object and use the pointer to the pointer to call size() member functio

How can I use a pointer to a pointer to a string object and use the pointer to the pointer to call size() member functio


By : user3287971
Date : March 29 2020, 07:55 AM
I hope this helps . Apply * once per indirection level, like in the declaration of the variable.
code :
std::string s = "tjena"; // A string
std::string *sp = &s; // A pointer to a string
std::string **spp = &sp; // A pointer to a pointer to a string
(**spp).size(); // Call it like this
(*spp)->size(); // Or like this
(*sp).size(); // Or like this
sp->size(); // Or like this
Tricks to analyse pointer and pointer-to-pointer structures in C?

Tricks to analyse pointer and pointer-to-pointer structures in C?


By : user3448259
Date : March 29 2020, 07:55 AM
I wish did fix the issue. My favorite trick for situations in which I lose track of pointers, is using pen and paper.
Draw rectangles for variables and arrows for pointers.
code :
data [ 0 ][ 1 ][ 2 ][ 3 ][ 4 ]
       ^    ^    ^    ^   ^^
       |    |    |    |.../|     ("..." meaning chronologically progressing,
       |    |    |    |  / |            though this one is the fourth step)
       |    |    |    | /  |
p    [ | ][ | ][ | ][ |/][ | ]

       ^    ^    ^    ^ 
       |... |... |... |             (here are the first three steps)
      ptr
Related Posts Related Posts :
  • How do I know it is OK to free the pointer after passed it to a function?
  • How to get the incoming ssh/telnets clients local IP address from a shell script or C
  • C: What does if(a<b<c) do?
  • What does this mean exactly in C?
  • What is the best way to achieve sscanf-like functionality in Perl?
  • How to create threads and sort correctly in a odd-even sorting program?
  • What's the single best reference on the topic of (POSIX) signals?
  • Avoid Overflow when Calculating π by Evaluating a Series Using 16-bit Arithmetic?
  • How to handle data or char -1 when reading from file, since EOF is also -1
  • Compare every 1kb of contents of two files instead of character by character
  • Understanding functions in stm8s disassembly?
  • Recursive and no-recursive get different answer
  • Can anyone explain the following output of the code?
  • How to resync time from NTP server in esp-idf?
  • Output for the following C code is confusing me
  • Determine if window is visible with High CPU
  • Writing a file by taking input from user only saves value for "x" only
  • How should I fill an array of ints from a file using command line args? The size of the file and the no. of elements may
  • Segmentation Fault While Reading File in C
  • Fill an array at index n with m times data without bit-fields
  • Why don't the values I type in the terminal match the values I've already stored in the array?
  • MPLABX XC16 unable to resolve built-in identifier __builtin_dmaoffset
  • How call and compile function from elf to my binary?
  • Modifying non-const char array that is referred to by const char array
  • Does anyone see any mistakes here? I am trying to get a message via a pipe, from my parent to child
  • Memory Leak - Singly linked list in C
  • Trouble with translation of typedef between C to D
  • Why does SIGINT stop sleep more than one time?
  • How to pause a timer created with CreateTimerQueueTimer
  • How can I read data from file ? I can't fix it
  • Scanf in visual studio not accepting multiple cases of characters
  • How can I determine if a char* is a windows line ending?
  • I am trying to store value in long long int type but giving wrong return
  • How to manage different kinds of data in a linked list?
  • uint8_t not rollover to 0 after reaching 255 not working properly
  • Why do these two execvp produce different results?
  • Pass uintmax_t or size_t to custom printf conversion specifier
  • Why does free() leaves stuff in memory?
  • Why pointers can't be used to index arrays?
  • memory allocation eror in C
  • C custom datatypes mapped to C datatypes grouped under a single struct
  • pipe() data is not transferred to child process
  • Getting a core dump from a simple C program
  • Fatal error on makefile, need to understand the problem
  • How can I add a delay of 90 minutes when a port has gone from 0 to 1?
  • To use strcpy or not
  • the usage of strtok() in c shows warnings and returns segmentation fault(core dumped)
  • Trouble allocating array of structs
  • Only first char of user input used in array
  • Why does "int x = 5; printf("%d %d %d", x==5, x=10, x==5);" in C print "0 10 0"?
  • How to scan specific string format in C?
  • sscanf skipping the final value when reading multiple values from a line
  • How can I access full memory space in FreeDOS with C application
  • Semantics of sem_getvalue() in POSIX
  • What does the [x,y] symbol mean in a multidimensional array access?
  • compilation error: cast from pointer to integer of different size
  • why am i getting compilation error "error: conflicting types for ‘ptr’ " for the following code? static int va
  • Why does getw return -1 when trying to read a character?
  • Why C program in whch two functions call each other recursively gives segmentation fault on linux?
  • ssize_t is undefined
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk