struct

What is actually going on in C when a non-pointer value is stored? [duplicate]

六月ゝ 毕业季﹏ 提交于 2019-12-25 19:38:38
问题 This question already has answers here : Pointer to pointer clarification (16 answers) Closed last year . IMPORTANT: This tried to ask too many things at once and was misleading because I wrote it under a false assumption about how pointers can be used, and it ended up just looking like a duplicate. Please see this instead: How are variables tied to their values in C? Let's consider that there is a value 4 at address 0001 , and then we assign the address 0001 to the variable num . We could

Failed to allocate an array of pointers to a struct

倖福魔咒の 提交于 2019-12-25 19:35:20
问题 I'm trying to allocate an array of pointers to a struct but there's something wrong with my code. This is my struct: struct Brick { GameObject2D* handle_; }; Brick** bricks_; And this is how i'm trying to allocate memory for it: int bricks_amount_ = 10; bricks_ = (Brick**)malloc(sizeof(Brick*) * bricks_amount_); The program crash. I've make a devenv on it to debug where's the problem and it crash on this line: for (unsigned short int i = 0; i < bricks_amount_; i++){ bricks_[i]->handle_ = new

Python 2.7 - clean syntax for lvalue modification

梦想的初衷 提交于 2019-12-25 19:04:42
问题 It is very common to have struct-like types that are not expected to be modified by distant copyholders. A string is a basic example, but that's an easy case because it's excusably immutable -- Python is unusual in even allowing things like method calls on literal strings. The problem is that (in most languages) we frequently have things like, say an (x,y) Point class. We occasionally want to change x and y independently. I.e., from a usage perspective, a Point LVALUE should be mutable (even

Trying to read a single character at a time into an array of indefinite size

社会主义新天地 提交于 2019-12-25 18:59:10
问题 I am a CS student working on a c++ project. We have been instructed to declare a struct and use it to read in an array of chars and keep a tally of how many letters are used in the string. We are not allowed to use a string; it MUST be an array of our declared struct. The input must be as long as the user wants; the code has to be able to accept new lines of input and be terminated by '.' I'm really struggling here. I don't even know where to begin. I've thrown together some code as best

PInvoke, function call with pointer-to-pointer-to-pointer parameter

℡╲_俬逩灬. 提交于 2019-12-25 18:32:53
问题 I am creating a new question here as I now know how to ask this question, but I'm still a newb in PInvoke. I have a C API, with the following structures in it: typedef union pu { struct dpos d; struct epo e; struct bpos b; struct spos c; } PosT ; typedef struct dpos { int id; char id2[6]; int num; char code[10]; char type[3]; } DPosT ; and the following API function: int addPos(..., PosT ***posArray,...) the way I call this in C like this: int main(int argc, const char *argv[]) { ... PosT *

Malloc a struct at a specific point in memory?

家住魔仙堡 提交于 2019-12-25 18:26:51
问题 I'm trying to create a struct at a specific location in memory: struct inRAM *ptr = (struct inRAM*)malloc(sizeof(struct inRAM)); But this line only allocates the memory at a place in RAM that is not retainable. I need to malloc beginning at a specific memory address for it to work properly, but how? 回答1: For embedded systems where you need to access specific memory addresses for I/O, you normally write directly to the address. You don't need to malloc here, that's used to manage blocks of

Freeing a double pointer from a struct

依然范特西╮ 提交于 2019-12-25 18:26:27
问题 I have a problem with my delete_table function. So i have 2 structs struct _entry_ { int key; int data; struct _entry_* next; struct _entry_* prev; }; typedef struct _entry_ entry; struct _table_ { entry** entries; int size; }; typedef struct _table_ table; I initialise my table with calloc. void table_init(table* ht, int initial_size) { ht->entries = (entry**)calloc(initial_size, sizeof(entry*)); if (ht->entries) { ht->size = initial_size; } } Now my free function that i wrote void table

PInvoke Pointer to struct including float array

孤者浪人 提交于 2019-12-25 18:14:32
问题 I'm attempting to P Invoke a C library for use on a Xamarin android app. Consider the following C structure: typedef struct { bool myBool; myOtherStruct sOtherStruct; int myInt; float myFloat; float myFloatArray[1024]; float myFloatArray2[1024]; float myFloatArray3[20]; float myFloatArray4[30]; } sMyStruct; This is called using the following function: unsigned int initialise(sMyStruct* a_pMyStruct) I've put this into a C# structure: [StructLayout(LayoutKind.Sequential)] public unsafe struct

Program crash after trying to access and write to structure member

蹲街弑〆低调 提交于 2019-12-25 17:43:14
问题 Working in Visual Studio in C and trying to do fft of some samples. When I attempt writing some value to member of struct my program crash and I get error access violation writing location 0x00000000. First, I tried to use this C code, but got errors: kiss_fft_cpx *cx_in = new kiss_fft_cpx[nfft]; kiss_fft_cpx *cx_out = new kiss_fft_cpx[nfft]; in this two lines. Okay there is no new in C. I tried to modify it but I can not do it. I tried kiss_fft_cpx *cx_in[1024]; kiss_fft_cpx *cx_out[1024];

c - Segmentation fault when trying to write a modified structure

↘锁芯ラ 提交于 2019-12-25 16:43:47
问题 This is in a program meant to work with ppm image files. I'm getting a compilation error when trying to modify a struct and then writing it to a new file. This is the global struct (declared in ppmIO.c and ppmIO.h): ppmIO.c: struct Image *instance; ppmIO.h: struct Image { int width; int height; unsigned char *data; }; extern struct Image *instance; This is my imageManip.h file: #include <ppmIO.h> void ImageInvert(struct Image **toInvert); void ImageSwap(struct Image **toSwap); These are the