Questions tagged [pointers]
18412 questions
-1
votes
1
answer
20
Views
subtracting two pointers(arrays), (C language) [duplicate]
This question already has an answer here:
Pointer Arithmetic In C
2 answers
Pointer subtraction confusion
8 answers
int vector[] = { 28, 41, 7 };
int *p0 = vector;
int *p1 = vector + 1;
int *p2 = vector + 2;
I know result of
printf("%p, %p, %p\n", p0, p1, p2);
is ex) 100, 104, 108
but why is the r...
0
votes
0
answer
4
Views
AddressSanitizer errors when testing the performance of singly linked list
I am working on a creating a singly linked list. To create that I have wrote structures such as struct node and operation like list_free_node. Now this is my first time using malloc and pointers, and I get a few errors (I think related to those two mentioned concepts) that I have never seen before....
2
votes
2
answer
20
Views
Is it bad practice to store a struct member value in local var with a shorter name?
I used a struct to store some data that I read from a file. The struct members are a float and an array of unsigned integers. After the file is read none of the values will ever change. The value I want to store locally is is one of the unsigned integers contained in the struct member array.
Is it...
0
votes
1
answer
17
Views
Finding and printing the last word of a string with a space or tab delimiter
I'm trying to get the last word of a string as an argv parameter in C. How can I find the last word and delimit the strings that contain a space/tabs?
The definition of a word is "a section of string delimited by spaces/tabs or by the start/end of the string." I initially made a function to identify...
0
votes
0
answer
23
Views
Struct pointer strange behavior
I'm trying to make a BrainFuck (BF) interpreter using a two-way linked list. However, the way I'm moving through this list seems to have a rather strange impact on cells.
So first here's the part of my program that gives me a hard time
int main(int argc, char **argv)
{
Cell c = {NULL, NULL, '\0', NU...
-1
votes
0
answer
13
Views
GDB shows valid pointer but function is called for invalid one
I get very strange behavior in my code. Including a lot of code is not an option so it can be boiled down to this. I have a class with member pointer to another instance of this class called parent (it is nullptr by default). And a function that is called recursively:
class FileEntry
{
void on_child...
2
votes
3
answer
77
Views
Containers vs Smart pointers in C++
How can I decide when choosing between std::containers (std::vector or std::array) and smart pointers pointing to arrays
I know containers are objects for memory managment. They are exception safe and there will not be any memory leak and they also provide veriuty of functions for memory managment(...
1
votes
2
answer
240
Views
How Qt manage the memory of an Widget pointer which is returned from the function QItemDelegate::createEditor()
I am checking the Qt example Spin Box Delegate example. In the example
QWidget *SpinBoxDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &/* option */,
const QModelIndex &/* index */) const
{
QSpinBox *editor = new QSpinBox(parent);
...
return editor;
}
How is the pointer editor del...
-1
votes
0
answer
24
Views
My pointer is still NULL even though I am assigning a non-null pointer to it
I don't understand why my code doesn't work. As you can see in the picture below, Xcode displays what has an address and what doesn't. It works on my friend's laptop perfectly, but it doesn't for me.
I am trying to split the linked list into 2. MyList1 contains the first half the list while MyList2...
1
votes
1
answer
22
Views
How to reset a pointer
After playing with a pointer given by malloc.
uint8_t* mem = malloc(10);
uint8_t* rst = mem;
*mem++
0
votes
1
answer
16
Views
How do you create a LinkedList, which contains loops, in Python?
I'm trying to come up with a test case for the solution given by hackerrank for detecting cycles in a linkedlist with python. The solution given by hackerrank is:
class Node(object):
def __init__(self, data = None, next_node = None):
self.data = data
self.next = next_node
def has_cycle(head):
fast =...
-1
votes
1
answer
52
Views
What is the use of Pointer to an array
What is the use of having pointer to an array when we can have single array of pointer.
instead of using pointer to an array we can use size 1 array and same result is expected.
int main()
{
int arr[] = {20,30,40,50,60,70};
int (*ptr)[6] = &arr;
int *prtarr[1] = { arr };
for (int i = 0; i < sizeof(a...
2
votes
1
answer
22
Views
Relation between pointers and arrays
I was pretty sure that *(pointer+1) = pointer[1].
My problem is that when i run this code:
if (NULL == (*str_array = (char **)malloc(sizeof(char *)*10))) {
return ERROR;
}
splitter = strtok(str, TOKEN);
while(splitter != NULL){
if (NULL == ((*str_array )[i] =(char *)malloc(sizeof(char)*strlen(str) +...
-1
votes
1
answer
23
Views
More C Pointer Troubles
I have been studying data structures and algorithms, sadly in C. I have separately implemented a doubly linked list which holds integers and works fine, but I am having a lot of trouble getting it to work properly when a node (or pub in this case) holds multiple values of different types. I can crea...
1
votes
2
answer
100
Views
Calling `delete` on an object owned by a `unique_ptr` using another pointer
I have a pointer to class initialized by the new operator. Then I use this pointer to setup a std::unique_ptr. Now, as far as my understanding goes, the following code has double delete, once the manually called delete operator and then when the unique pointer goes out of scope. How does this code r...
1
votes
1
answer
43
Views
Iterating through a vector of stucts's members with pointers and offsets Part 2
So to this is part 2 of a question I asked and was answered yesterday. So today I am coming back with a part 2. I am not sure if this should be this somewhere else so if a moderator wants to move it feel free.
So I am not going to reintroduce my problem here, so please go read part 1
Iterating throu...
1
votes
3
answer
30
Views
Pointer to an array type (custom types)
I have a custom array type defined as in the code and a pointer to that array type also defined. I'm trying to find a correlation between the custom array type and a pointer to the custom array type. How do I assign a value to the pointer of the custom array type so I can iterate through the array u...
0
votes
3
answer
41
Views
How are 2d Array and pointers related?
Why is these two are same things I saw one answer to this similar question but couldn't really understand.
Why *(a+i) and a+i doing the same work.
int a[1][2] = {1,2};
printf("%p or %p\n",*(a+0),a+0);
1
votes
2
answer
6.4k
Views
How to compare two unsigned integers (uint8_t) in c
I have two uint8_t pointers in my program and I want to compare the values of those. I don't have good idea to deal with unsigned ints. Here is my code
static void bacast_signed_message()
{
uint8_t *M = malloc(MAX_M_LEN*sizeof(uint8_t));//uint8_t M[MAX_M_LEN];//uint8_t *M;
int M_len = MAX_M_LEN;;
ui...
1
votes
2
answer
2.3k
Views
Incrementing pointers of pointers
I'm trying to figure out how i would go about incrementing a pointer of pointers to point at a value i want. This is essentially an array of pointers to cstrings and i want to increment the pointer to point at xx pointer. For insistence in my current application if i want to point at the fifth point...
-1
votes
2
answer
31
Views
Why pointer not giving its Ascii Value
here is my Code.
#include
int main() {
/* code */
char a[5] = {'a','b'};
int *p =a;
printf("%d\n", *p);
return 0;
}
So here, When I execute its showing "25185" instead of giving a ascii value.
Why is it so?
Thank you
0
votes
1
answer
14
Views
Extracting the real part of an UnsafeMutablePointer as a Float and storing that on an array
I have this function
func arrayForPointer(_ pointer: UnsafePointer, count: Int) -> [T] {
let buffer = UnsafeBufferPointer(start: pointer, count: count)
return Array(buffer)
}
and this call
let arrayComplex = self.arrayForPointer(&output, count: 4)
I want to enumerate thru arrayComplex and extract th...
1
votes
0
answer
45
Views
In what condition we should use pointer to pointer?
/* set *largest_pt to the largest element pointed to by the array */
void find_largest(int **A, int A_size, int *largest_pt) {
*largest_pt = **A;
for (int i = 1; i < A_size; i++) {
if (*A[i] > *largest_pt) {
*largest_pt = *A[i];
}
}
}
In this example, I don't know why we use pointer to pointer **A....
1
votes
1
answer
96
Views
Array of constant pointers to functions
I want to make an array of constant pointers to functions.
Something like this:
#include
#include
int f( int x);
int g( int x );
const int ( *pf[ ] )( int x ) = { f, g };
int main(void)
{
int i, x = 4, nf = 2;
for( i = 0; i < nf; i++ )
printf( "pf[ %d ]( %d ) = %d \n", i, x, pf[ i ]( x ) );
retur...
1
votes
3
answer
859
Views
Dummy Node in Linked List in C++
I am trying to develop a way in which I can solve Linked list problems without having to care about the head node in any special way i.e. In linked list problems we usually deal with the head pointer separately before we start with the next nodes.
I found a way: Use a dummy node so that the actual l...
1
votes
1
answer
83
Views
C Pointer Arithmetic Strange Behavior
I have the following seemingly simple piece of code:
void freeBin(MallocHeader * pBinHeader){
while(pBinHeader){
MallocHeader * pNext = pBinHeader->pNext;
pBinHeader->magic = K_MAGIC_NUM;
printf("Orig magic and size = %i %lu\n", pBinHeader->magic, pBinHeader->size);
void * freeAddr = pBinHeader + si...
4
votes
4
answer
133
Views
declare and define function pointer variable in one line
... apologies for the previous one
In C++ how do we do the following
// fundamental language construct
type name = value ;
// for example
int x = y;
with function pointers?
typedef (char)(*FP)(unsigned);
// AFAIK not possible in C++
FP x = y ;
I can use lambdas:
FP x = []( unsigned k) -> c...
5
votes
2
answer
68
Views
Is (*exptr)->cnt the same as exptr->cnt or (*exptr).cnt?
Is (*pointer)->name the same as pointer->name or (*pointer).name?
0
votes
0
answer
25
Views
Array of pointers to functions with variable number of arguments
I have the following task to do on my studies:
I have some different methods of sorting such as bubble sort, quick sort, etc.
I have to create an array of pointers, each of them pointing to one of given methods. Then I can execute all sorting methods with for loop.
However, not all sorting functions...
1
votes
1
answer
58
Views
I succeeded allocating a pointer to an array of 3 ints in the heap, but I still don't understand how does the access to the array work?
After several trials I've succeeded allocating a pointer to an array of 3 ints with a new expression. In the snippet below, I show first the use of this pointer in the stack, and then I show where I've got with the heap allocation, after several trials.
#include
int main()
{
// First, pointer p to...
1
votes
1
answer
223
Views
Calling a procedure by pointer from variable in Intel 8086 Assembly
Let's say I have a variable with procedure address:
func_pointer dw offset my_function
my_function proc near
my_function endp
How can I call it from the variable?
I have tried
call dword ptr[func_pointer + 1]
but it does not seem to work.
1
votes
1
answer
1.7k
Views
return array of pointers from an array in Go
Just so you know, I am quite new to Go.
I have been trying to make a function like this:
func PointersOf(slice []AnyType) []*AnyType{
//create an slice of pointers to the elements of the slice parameter
}
It is like doing &slice[idx] for all elements in the slice, but I am having trouble with how to...
1
votes
1
answer
84
Views
How offset of structure is calculated? Explain printf statement here?
#include
main()
{
unsigned char c;
typedef struct name {
long a;
int b;
long c;
}r;
r re = {3,4,5};
r *na = &re;
printf("%d", *(int*) ((char*)na + (unsigned int) & ( (struct name *)0 )->b));
}
OUTPUT :
4
I would be thankful if somebody explain in detail what printf statement is doing in this c pro...
1
votes
3
answer
278
Views
What exactly int/char when we declare double pointer(**q)
Lets take 64 bit machine
where pointer is of 8 bytes in 64 bit machine
int *p ; // it is a pointer to integer variable so when i increment p
// i.e., p++ it will increment by 4
char *r; // It is pointer to character .
// So if i increment 'r' it will increment by 1
int **q ; // if i increment q...
1
votes
3
answer
447
Views
Accessing a struct by index
I'm being passed a pointer to a struct, and the first 8 members are the same size, can I access them by index?
typedef struct example{
uint64_t one;
uint64_t two;
uint64_t three;
uint64_t four;
uint64_t five;
//etc...
uint8_t ninth;
} example_t;
void example_method(example_t *ptr)
{
//can I do this?...
1
votes
1
answer
1.6k
Views
Are smart pointers thread safe?
A smart pointer is an abstract data type that simulates a pointer while providing added features such as automatic memory management or bounds checking.
My question is, are they thread-safe?
1
votes
2
answer
889
Views
Golang how to range in pointer array
if I have PersonManager struct and it has *[]Person array. I want to range every item in this array. For example
manager := *PersonManager
for ii := 0; len(*manager.allPersons); ii++{
fmt.Println(manager.allPersons[:ii].name)
}
for this example, manager variable is pointer and array that in this var...
0
votes
1
answer
51
Views
Creating a smart pointer member of class referencing itself is a elegant design pattern in c++?
I would like to know if following code is a good pattern in C++?
There is no problem at all. The code works. But I would like to know if this can lead to some sort of problem.
#include
#include
template
class Class {
public:
std::shared_ptr shared_ptr;
Class() : shared_ptr(this) {}
~Class() { sha...
1
votes
1
answer
110
Views
Gob can't encode map with a nil pointer value
Gob's Encode returns an error when I try to encode a map to pointers if one of the values is nil. This seems to contradict the docs (but I may be misinterpreting the meaning):
In slices and arrays, as well as maps, all elements, even zero-valued elements, are transmitted, even if all the elements ar...
1
votes
1
answer
49
Views
Create a Dynamic array (calloc) in a function, and use it in main [duplicate]
This question already has an answer here:
Changing array inside function in C
6 answers
Sorry to open a new question, but I can't find a question like this in the forum or around google..
Anyway, my problem is this:
Inside Main I declare an array "insieme_A" and a variable that contains the lenght...