60 questions
-2
votes
3
answers
187
views
C++ define a type alias 'pointer to array' with keyword using (not typedef) [duplicate]
It is easy to achieve this with typedef:
typedef int (*ptr_arr)[]; // pointer to array
But how to alias the same type as ptr_arr by using C++ using keyword?
Also would be nice to see (good formatted)...
0
votes
1
answer
176
views
C++ pointer-to-member if the member is an array
I have a structure with several members of unsigned short type. The values stored in these types should be in a certain range, so I use a setter method for them that checks the range of the value ...
0
votes
2
answers
40
views
Evaluation order of &a[0]
Assume one dimensional array a={1,2,3,4} with base address as 100.Find the value of p.
P=&a[0];
I know it will give tha base address of 0th element of 1D array. But in which order c compiler will ...
0
votes
0
answers
1k
views
How to get values from aggregation cursor which is not an array Nodejs Mongodb
I have an aggregation like this :
this.collection.aggregate([
{
"$match": {
_id: id
}
},
{
"$addFields": {
"self&...
24
votes
2
answers
2k
views
Allocating less memory than the specified size of a pointer-to-array
In C, is it "legal" to under-allocate memory to a pointer-to-array if we then only access elements that fall within the allocated memory? Or does this invoke undefined behavior?
int (*foo)[ ...
1
vote
2
answers
110
views
What is the difference between int (*p)[10]=s and int (*o)[5]=&s?
On basis of the convention int (*o)[5]=&s; is the right way for a pointer o to point an array having 5 elements.
We can also write this s in this statement
int (*p)[10]=s;
but why preferring
&...
0
votes
2
answers
645
views
How can you dynamically allocate a pointer in the form int (*p)[n] in C? [closed]
Suppose I get input from somewhere and store it in a variable. Pretend the variable is Cols.
I want to make an array in the form
int (*p)[Cols]
Now, I know you can't do this. So how would I ...
0
votes
1
answer
90
views
difference between (*)[] and * in C [duplicate]
would love for some help with understanding the difference between these two lines:
int(*p)[20] = (int(*)[20]) malloc(sizeof(int) * 20);
int* y = (int*)malloc(sizeof(int) * 20);
if I do:
int * tmp = ...
8
votes
5
answers
6k
views
What is a pointer to array, int (*ptr)[10], and how does it work?
int (*ptr)[10];
I was expecting ptr to be an array of pointers of 10 integers.
I'm not understanding how it is a pointer to an array of 10 integers.
1
vote
3
answers
264
views
Why does the declaration int (*f())[]; doesn't give an error or warning?
The following statement declares a function which takes no argument and returns a pointer to an integer array.
int (*f())[];
It returns a pointer to an integer array of size _____
We didn't specify ...
0
votes
1
answer
38
views
Using strings or pointers to strings in struct
I think I am missing something critical and I can't find an answer. If I want to use string in struct, is it better to use char arrays or pointers to them?
Following code works as intended, but raises ...
1
vote
2
answers
1k
views
How could I swap rows in a 2D array defined using pointers to array?
I want to swap two rows of a 2d array defined as bellow.
double (*mat)[N];
mat = (double(*)[N])malloc(m*sizeof(double [N]));
...
swap(mat, mat+1);
But my swap(mat, mat+1); only swaps ...
1
vote
2
answers
2k
views
C++ Getting neighbours of a cell in a grid, -1x throwing null exception when checked if != NULL
recently moved from C# to C++ so I'm new to pointers and references and so on.
I've a pointer-to-pointer array declared like this
enum Type
{
Void,
DeepWater,
Water,
... etc }
Tile::...
2
votes
1
answer
178
views
Initializing pointer to array without & operator?
In the following pointer to array,
int x[5] = {1};
int (*a)[5] = &x;
what implications and consequences can i have if i don't use & operator and do like this..
int x[5] = {1};
int (*a)[5] = ...
3
votes
2
answers
148
views
How to use pointer to multi dimentional array in C language?
I have 2-D array
char arr[2][3]={"sam","ali"}
and pointer to this array
char(*ptr)[3]=arr;
How can I use this pointer to print arr[2][2] which in this case is i.
I've tried * (*(ptr+1)+2) the same ...
-1
votes
1
answer
54
views
Character Pointer to strings
This is C code snippet:
int main()
{
char *names=[ "tom", "jerry", "scooby" ];
printf("%s", *names[0]);// prints t
printf("%s", *names[1]);// prints j
// how to print full word "tom", or ...
0
votes
1
answer
143
views
Pointers to Pointers to Avoid Duplicate Code?
I am new to learning about C, so as an exercise I attempted to create a text-based card game where each person/CPU places down a random card at the top of their deck, and the person with the higher ...
3
votes
1
answer
599
views
Array pointer in C program
Here is a C program in textbook, it asks a 3*5 2D array from users and prints the third line.
I am confused with int* p[5]. Why here needs to have [5], I think just int* p is OK. It can repeatedly ...
0
votes
1
answer
194
views
Passing pointer-to-char in function: syntax error
My question is related to my previous post:
explicit specialization: syntax error?
I am trying to pass arrays of pointer-to-chars as an argument to a function (which I will later incorporate to a ...
1
vote
2
answers
57
views
What value does a pointer to pointer get assigned when points to a dynamically allocated memory?
Consider the following case:
int **my_array = new int*[10];
What do we assign to my_array here?
my_array is a pointer that points to what?
Is there any way to iterate through my_array (the pointer) ...
2
votes
6
answers
100
views
How is pointer to array different from array names?
I was reading more about arrays vs pointers in C and wrote the following program.
#include <stdio.h>
int arr[10] = { } ;
typedef int (*type)[10] ;
int main()
{
type val = &arr ;
...
3
votes
1
answer
336
views
Pointer to array and errors C2057, C2540
I want to do something like:
const int N = 10;
void foo (const int count)
{
int (* pA) [N][count] = reinterpret_cast<int(*)[N][count]>(new int[N * count]);
...
}
But my compiler (VS2010) ...
2
votes
1
answer
155
views
How to make a pointer to a vector? I work on a vector inside the function and I want to keep the values
I am writing a C function so that when called, the function would return an array (vector) of the first p prime numbers(with p given from the keyboard in main).
My problem is when I try to call the ...
1
vote
4
answers
107
views
Check the following code. Its related to the pointer to an array concept
Consider the following code
#include<stdio.h>
void main()
{
int s[4][2] =
{
{20,1},
{21,2},
{22,3},
{23,5}
};
int (*p)[2];
int i,...
0
votes
0
answers
33
views
copying data from pointer-to-array to pointer-to-array
How would I go about copying the content of a pointer to an array to a temporary pointer to an array, and then making the original pointer point to the same thing the temporary pointer is pointing too?...