double-pointer

Using single versus double pointers in Linked lists implemented in C

…衆ロ難τιáo~ 提交于 2019-12-14 01:24:42
问题 I was writing this code for adding element at the end of linked list: struct node{ int info; struct node* link; }; void append ( struct node **q, int num ) { struct node *temp, *r ; if ( *q == NULL ) // if the list is empty, create first node { temp = (struct node*) malloc ( sizeof ( struct node ) ) ; temp -> info = num ; temp -> link = NULL ; *q = temp ; } else{ temp = *q ; /* go to last node */ while ( temp -> link != NULL ) temp = temp -> link ; /* add node at the end */ r = (struct node *

Why in C++ do I need to pass a pointer by reference to change the pointed content?

元气小坏坏 提交于 2019-12-13 10:42:14
问题 I am writing a function to load text from shader code file. I have stumbled upon something strange regarding pointers and I cannot figure out why. I have a function named Load. In this function I copy text, taken from a file stream, in the output variable. static void Load(const GLchar* source_path, GLchar* output,GLint& count ) { string code; // loading logic here code= vShaderStream.str(); // copying the string from the stream count = code.length(); output = new GLchar[count]; std::size_t

How do the double pointers (struct tree_st **root) actually work? [duplicate]

拥有回忆 提交于 2019-12-13 08:31:19
问题 This question already has answers here : Why use double indirection? or Why use pointers to pointers? (17 answers) Closed 2 years ago . We have to create a binary tree with car models and modify it in different ways. The main problem for me is the use of a double pointer ( struct tree_st **root ). Why can't I just use a standard, single pointer? Here's the code if you need more details of what I'm talking about: #include<stdio.h> #include<stdlib.h> #include<string.h> #define MAX_SIZE 20+1

Accessing a 2D array using double pointer to function C language

瘦欲@ 提交于 2019-12-13 06:48:41
问题 I am trying to find the maximum of all values in a 2D array accessed from a C function with the help of double pointer. When I run the code it just terminates with returning any value to the caller function. I tried to change the code to print all values to find out the problem and found that it only prints 1 and 2 for the following sample data as input. For a sample code run, I provided row=2, col=2 and values=1,2,3,4 Please let me know why? Also if you question is not clear please say so. I

Mergesort - Segmentation fault

扶醉桌前 提交于 2019-12-11 06:09:54
问题 Code directory structure, ./Computing$ ls -LR .: list file.txt mergeSort.c program.exe type.h ./list: arrayImpl.c linkedListImpl.c list.h Compilation procedure: $./Computing gcc -Wall -Werror -DARRAY -I. mergeSort.c ./list/*.c -o program Here is the complete code having files, mergeSort.c , list/* , type.h With the given representation of List , typedef struct List{ void **array; /* Housekeeping - Array enhancement/shrink */ int lastItemPosition; int size; }List; mergesort is performed below

how to declare variable of NSString with double pointer

落花浮王杯 提交于 2019-12-07 02:47:13
问题 I wanna use double pointer and I tried to declare like this. NSString **a; but, Xcode showed me the error "Pointer to non-const type 'NSString *' with no explicit ownership" and it couldn't be compiled. Finally I wanna do like this. NSString **a; NSString *b = @"b"; NSString *c = @"c"; a = &b; *a = c; NSLog(@"%@",b);//I wanna see "c" Let me know any advise please. 回答1: Change to this so that you can explicitly specify the ownership: NSString *__strong *a; NSString *b = @"b"; NSString *c = @"c

usage of double pointers as arguments

此生再无相见时 提交于 2019-12-01 09:17:58
Please find the code snippet as shown below: #include <stdio.h> int My_func(int **); int main() { int a =5; int *p = &a; My_Func(&p); printf("The val of *p is %d\n,*p); } void My_Func(int **p) { int val = 100; int *Ptr = &val; *p = Ptr; } How does by using a double pointer as a argument in my_Func function and making change of value reflects the same in the main function but if we use a single pointer in My_Func does not change the value in main?Please do explain me with examples if possible Advanced thanks Maddy int **p is a pointer to a pointer-to-int. My_Func(int **p) works by changing the

How to get size of 2D array pointed by a double pointer?

可紊 提交于 2019-11-30 15:39:12
问题 I am trying to get the number of rows and columns of a 2D Array from a double pointer pointed to the array. #include <stdio.h> #include <stdlib.h> void get_details(int **a) { int row = ??? // how get no. of rows int column = ??? // how get no. of columns printf("\n\n%d - %d", row,column); } Above function needs to print the details of the size, where am going wrong. int main(int argc, char *argv[]) { int n = atoi(argv[1]),i,j; int **a =(int **)malloc(n*sizeof(int *)); // using a double

How to get size of 2D array pointed by a double pointer?

≡放荡痞女 提交于 2019-11-30 15:16:55
I am trying to get the number of rows and columns of a 2D Array from a double pointer pointed to the array. #include <stdio.h> #include <stdlib.h> void get_details(int **a) { int row = ??? // how get no. of rows int column = ??? // how get no. of columns printf("\n\n%d - %d", row,column); } Above function needs to print the details of the size, where am going wrong. int main(int argc, char *argv[]) { int n = atoi(argv[1]),i,j; int **a =(int **)malloc(n*sizeof(int *)); // using a double pointer for(i=0;i<n;i++) a[i] = (int *)malloc(n*sizeof(int)); printf("\nEnter %d Elements",n*n); for(i=0;i<n

Linked list head double pointer passing

冷暖自知 提交于 2019-11-29 05:15:17
I have seen this in some book/ tutorial. When you pass in the head pointer (of linked list) into a function, you need to pass it as a double pointer. For eg: // This is to reverse a linked list where head points to first node. void nReverse(digit **head) { digit *prev=NULL; digit *curr=*head; digit *next; while(curr!=NULL) { next=curr->next; curr->next=prev; prev=curr; curr=next; } *head=prev; return; } This works fine. It also works when I use single pointer like, void nReverse(digit *head) { digit *prev=NULL; digit *curr=head; digit *next; while(curr!=NULL) { next=curr->next; curr->next=prev