pointer-arithmetic

Pointer arithmetic in Go

泄露秘密 提交于 2019-12-17 23:26:58
问题 Considering you can (can't think of a great way to put it, but) manipulate pointers in Go, is it possible to perform pointer arithmetic like you would in C, say for iterating over an array? I know loops are just fine for that kind of things these days but I'm just curious if it's possible. 回答1: No. From the Go FAQ: Why is there no pointer arithmetic? Safety. Without pointer arithmetic it's possible to create a language that can never derive an illegal address that succeeds incorrectly.

Order of operations for dereference and bracket-ref in C

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-17 07:29:07
问题 If I do *ptr[x] , is that equivalent to *(ptr[x]) , or (*ptr)[x] ? 回答1: *(ptr[x]) See the Wikipedia operator precedence table, or, for a more detailed table, this C/C++ specific table. 回答2: In C, all postfix operators have higher precedence than prefix operators, and prefix operators have higher precedence than infix operators. So its *(ptr[x]) 回答3: Using the counter-clockwise movement of analyzing and parsing that simple example 1. starting with ptr, work in counter-clockwise until you hit

Pointer/Address difference [duplicate]

痞子三分冷 提交于 2019-12-17 06:53:06
问题 This question already has answers here : C/C++: Pointer Arithmetic (6 answers) Closed 2 years ago . Why is the difference between the two addresses wrong? http://codepad.org/NGDqFWjJ #include<stdio.h> int main() { int i = 10, j = 20; int *p = &i; int *q = &j; int c = p - q; printf("%d\n", p); printf("%d\n", q); printf("%d", c); return 0; } Output: -1083846364 -1083846368 1 回答1: First, pointer arithmetic isn't defined when performed on unrelated pointers. Second, it makes sense. When

Is the “one-past-the-end” pointer of a non-array type a valid concept in C++?

徘徊边缘 提交于 2019-12-17 06:49:48
问题 The C++ standard [sec 5.7] says: If both the pointer operand and the result point to elements of the same array object, or one past the last element of the array object, the evaluation shall not produce an overflow; otherwise, the behavior is undefined. So, am I correct in assuming that pointers one-past-the-end of other types than arrays are undefined? For example: int a = 0; vector<int> v(&a, (&a)+1); The above snippet compiles and works just fine (with g++), but is it valid? 回答1: No, it is

Does (p+x)-x always result in p for pointer p and integer x in gcc linux x86-64 C++

大城市里の小女人 提交于 2019-12-14 03:31:08
问题 Suppose we have: char* p; int x; As recently discussed in another question, arithmetic including comparison operations on invalid pointers can generate unexpected behavior in gcc linux x86-64 C++. This new question is specifically about the expression (p+x)-x : can it generate unexpected behavior (i.e., result not being p ) in any existing GCC version running on x86-64 linux? Note that this question is just about pointer arithmetic; there is absolutely no intention to access the location

C:Pointer Arithmetic -How does it work?

*爱你&永不变心* 提交于 2019-12-13 10:01:17
问题 I'm new to C programming and trying to understand how pointer arithmetic works. The below printf statement prints 2 when the arguments for printf is *(p+2) and 4 with for *p. Could you please explain this behaviour ? #include <stdio.h> #include <conio.h> int main() { int arr[4] = {4,3,2,1}, *p = arr; printf("\n%d", *(p+2)); return 0; } 回答1: Let's re-write your program to make it a little clearer: #include<stdio.h> int main(void) { int arr[4] = {4,3,2,1}; int *p = arr; printf("\n%d", *(p+2));

Addressing Memory in MPI_Gather C

隐身守侯 提交于 2019-12-13 08:10:40
问题 I am trying to pass on data to MPI_Gather . I allocate memory as follows: float *phie, *phitemp; MPI_Comm_size(MPI_COMM_WORLD, &size); MPI_Comm_rank(MPI_COMM_WORLD, &rank); phitemp=(float *) malloc(20*sizeof(float)); if (rank==1) phie=(float *) malloc(itermax*20*size*sizeof(float)); and then get all processes to send data to rank 1 using MPI_Gather() as below: for (iter=0;iter<itermax;iter++) { MPI_Gather((float *) phitemp, 20, MPI_FLOAT, (float *) (phie+iter*20*size*sizeof(float)), 20, MPI

c and objective-c — const char* and char*

可紊 提交于 2019-12-12 00:34:31
问题 I have a function: -(void)ADPCMDecode:(char *)indata : (short *)outdata :(long)len { indata is a char and the function does pointer arithmetic to iterate for a length of len, modifying outdata, which is a short and I will need to do pointer arithmetic to get the values from it. I am attempting to call the function using: const char *modulatedBytes1 = [modulatedAudio bytes]; char *modulatedBytes [] = modulatedBytes1; unsigned int moduleatedLength = [modulatedAudio length]; short

Which dummy pointer values are okay

时间秒杀一切 提交于 2019-12-11 21:09:44
问题 This is something that's been on my mind for a long time. Every once so often i see people use 1, or -1 as a dummy value for a pointer. To safe the need of a different variable. Is it okay to use pointers like this? I suppose -1 is okay. But is it also okay to use other negative numbers? And can I be sure 1 isn't a used memory location? And if so, up to which value can i use memory pointers. P.s. I'm aware storing other data especially positive values in pointers, is very, very bad practice.

Casting behavior in C

眉间皱痕 提交于 2019-12-11 05:22:47
问题 I'm currently trying to avoid the pointer arithmetic workings in C to write an emulator. Usually, if you add 1 to a pointer in C, you add the size of the pointed to object instead. However, I am trying to work with bits and bytes, so this is undesired. I was wondering if I was using too many parentheses in this example: *(int16_t *)(((intptr_t)bc)+sp) And if not, then is it equivalent to this? : *(int16_t *)((intptr_t)bc+sp) sp is a page-aligned stack address for my emulator (obtained via.