pointers

Extending and shrinking array using realloc

喜夏-厌秋 提交于 2021-01-28 15:50:55
问题 I'm trying to write a program which first dynamically initialises a queue array for 100 int elements. Whenever the queue is full and another element is supposed to be queued, the original array is supposed to double it's size so new elements can be inserted. In case elements are dequeued, and the amount of elements the queue consists of falls below half of its actual size, the queue size is supposed to be cut in half. However, its size should never fall below 10. I'm trying to expand and

Extending and shrinking array using realloc

被刻印的时光 ゝ 提交于 2021-01-28 15:42:29
问题 I'm trying to write a program which first dynamically initialises a queue array for 100 int elements. Whenever the queue is full and another element is supposed to be queued, the original array is supposed to double it's size so new elements can be inserted. In case elements are dequeued, and the amount of elements the queue consists of falls below half of its actual size, the queue size is supposed to be cut in half. However, its size should never fall below 10. I'm trying to expand and

Extending and shrinking array using realloc

岁酱吖の 提交于 2021-01-28 15:40:09
问题 I'm trying to write a program which first dynamically initialises a queue array for 100 int elements. Whenever the queue is full and another element is supposed to be queued, the original array is supposed to double it's size so new elements can be inserted. In case elements are dequeued, and the amount of elements the queue consists of falls below half of its actual size, the queue size is supposed to be cut in half. However, its size should never fall below 10. I'm trying to expand and

Using void pointer to simulate a generic linkedlist in C

ぃ、小莉子 提交于 2021-01-28 13:04:09
问题 I'm new to C, and I think there may be an issue with pointers here. Any help would be appreciated! I have a linkedlist struct that looks like this: ll.h: #ifndef LLTEST_LL_H #define LLTEST_LL_H #include <stdlib.h> typedef struct _listNode { void *data; struct _listNode *next; } listNode; typedef struct { int logicalLength; int elementSize; listNode *head; listNode *tail; } linkedlist; typedef struct table { const char* name; size_t col_count; size_t length; } table; typedef struct db { const

Deleting node in linked list - segmentation fault

ⅰ亾dé卋堺 提交于 2021-01-28 11:42:40
问题 Problem requires to delete node from linked list given head pointer of list and the position of node in list to be deleted. More details of question can be found at: https://practice.geeksforgeeks.org/problems/delete-a-node-in-single-linked-list/1 Code returns segmentation fault but not exactly sure where I went wrong. My code is as follows: Node* deleteNode(Node *head,int x) { //Your code here struct Node* temp = head; if(x==0){ //change head head = temp->next; free(temp); } //find previous

Assigning const int to a const pointer to int is illegal?

↘锁芯ラ 提交于 2021-01-28 11:24:07
问题 Why is the following illegal? extern const int size = 1024; int * const ptr = &size; Surely a pointer to non-const data should be allowed to point to a const int (just not the other way around)? This is from C++ Gotchas item #18 回答1: If you really meant one of const int * const ptr = &size; const int * ptr = &size; that is legal. Yours is illegal. Because it it wasn't you could do int * ptr const = &size; *ptr = 42; and bah, your const was just changed. Let's see the other way around: int i =

sigaction : using “void (*sa_sigaction)(int, siginfo_t *, void *);”

筅森魡賤 提交于 2021-01-28 10:01:18
问题 In sigaction manpage it's written : sa_sigaction also specifies the action to be associated with signum . This function receives the signal number as its first argument, a pointer to a siginfo_t as its second argument and a pointer to a ucon- text_t (cast to void* ) as its third argument. So we can pass arguments to the signal handler (throught void* ), but I can't find the way. Is there no way to put it anywhere? Example : struct ping_val { int data1; int data2; }; void ping(int sig, siginfo

Pointer valid out of scope? [duplicate]

爷,独闯天下 提交于 2021-01-28 08:35:03
问题 This question already has answers here : Can a local variable's memory be accessed outside its scope? (20 answers) Closed 5 years ago . There's a PDF I'm reading which says that a pointer is invalid after it passes out of scope. See slide #14 in the file below: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-087-practical-programming-in-c-january-iap-2010/lecture-notes/MIT6_087IAP10_lec05.pdf Now I wrote almost the exact same code below in C++ with Dev-C++ compiler:

Is this pointer type watertight?

北城以北 提交于 2021-01-28 07:56:06
问题 I'm trying to design a custom type that can be used in APIs which need window handles or other kinds of pointers, and which will work for all systems VBA can run on. Here's what I've got: #If (Win64 = 1) And (VBA7 = 0) Then Public Type LongLong '64-bit systems pre-VBA7 wouldn't have had LongLong LoPart As Long HiPart As Long End Type #End If Public Type Pointer 'could alternatively make a LongPtr type for pre VBA7 systems only #If VBA7 Then Address As LongPtr 'should always be correct right?

Define a struct with a member pointing to another member

吃可爱长大的小学妹 提交于 2021-01-28 07:02:10
问题 I'm trying to program a network in C. I have nodes which are linked to each other and I 'd like to do that by making the struct member point to another member (not to another node, because I want to preserve the identity of the links). The code I made to do that is something like: struct node{ int k; //number of links struct node.link **link; //<- wrong }; but this is not right as node is not a variable but a type of variable (this is already discussed as an error in another QA: first you