60 questions
-2
votes
3
answers
192
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
94
views
C Changing value of array of struct through reference
I asked a similar question before, when I didnt know what was the problem of my code. Just as I was recommended I will give it in a better format.
This is an example of what happens to my code.
#...
0
votes
1
answer
177
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 ...
3
votes
7
answers
627
views
Memory allocation for pointer to an array in c?
#include <stdio.h>
int main()
{
int (*ptr) [5];
printf ("%lu\n" , sizeof (ptr));
printf ("%lu\n" , sizeof (*ptr));
}
I am using 64 bit system. When I run this code in gcc ...
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 ...
1
vote
3
answers
210
views
Warnings of Pointer to array of matrices In ANSI C GCC C90
I'm having strange warnings in Linux.
In Visual Studio on Windows, the code compiles and works well but I need to compile it with GCC c90 I get these warnings:
I've initialized the matrix like this:
...
0
votes
0
answers
2k
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&...
1
vote
1
answer
149
views
Pointers to arrays and effective types
I’m confused about effective type in the case of pointers to arrays in C. Does accessing an individual member via a pointer to an array impart an effective type only on the memory for that member or ...
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)[ ...
0
votes
2
answers
216
views
Pointer-to-array, malloc, and out-of-bounds access
Given a pointer-to-array in C, can we malloc to it enough memory for extra elements (beyond the specified array size) and then safely access those elements using either the [] operator or pointer ...
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
1
answer
183
views
How to deal with undefined behavior of pointer to array in cpp?
I was learning about pointers and pointers to arrays, when I encountered this strange thing.
Can anyone explain why this works?
char str[] = "This is a String";
int *p = str;
while(*p) std::...
0
votes
2
answers
647
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 ...
-1
votes
3
answers
820
views
pointer to an array int (*ptr)[]
I am trying to understand how a pointer to an array works. A code snippet;
#include<stdio.h>
int main()
{
int arr[3] = { 0 , 8 ,10 };
int (*ptr)[3] = &arr;
int i = 0;
for (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 = ...
1
vote
2
answers
91
views
Modifying elements of an array through a pointer to its elements
Given the code:
int vector[5] = {1, 2, 3, 4, 5};
int *pv = vector, value = 3;
for(int i = 0; i < 5; i++) {
*pv++ *= value;
}
for(int i = 0; i < 5; i++) {
printf("%d, ", *(pv+...
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.
0
votes
2
answers
126
views
Ommiting second dimension for 2-d array in formal argument doesn't throw any error or warning
I was going through the answers to this question Why is it allowed to omit the first dimension, but not the other dimensions when declaring a multi-dimensonal array? and some other questions as well ...
1
vote
3
answers
273
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 ...
1
vote
2
answers
271
views
confused about pointer syntax when defining type in C
In some starter code we have:
/**
* RGBTRIPLE
*
* This structure describes a color consisting of relative intensities of
* red, green, and blue.
*
* Adapted from http://msdn.microsoft.com/en-...
0
votes
1
answer
47
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
1
answer
103
views
Pointer to array of runtime-determined size pre-C99
Before C99, does the C standard allow defining or casting to pointers to arrays of length determined at runtime?
I understand that the standard doesn't allow variable length arrays before C99, but ...
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
3
answers
142
views
Array and Array[0] confusion
In the following C code:
char test[] ={'T','e','s','t'};
printf("%d\n",test == &test[0]); // Returns 1 - Okay as array varaible holds address of first element
So shouldn't the following should ...
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::...
0
votes
1
answer
254
views
How does a pointer to a 2D array work underneath the hood?
I can't understand why this piece of code which is supposed to perform matrix multiplication goes wrong.
Input: 2x2 matrices with elements 1,2,3,4 in both matrices
Expected output: 7 10 15 22
...
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] = ...
2
votes
2
answers
1k
views
How to define and use a pointer to an "array" member?
I have a bunch of structs, each of which has an 'array' member and a size indicator:
struct S {
size_t num;
int arr[100];
};
struct V {
float array[10];
int size;
};
I want to create ...
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 ...
2
votes
2
answers
101
views
While dereferencing pointer to an array I am getting same address as that of pointer to an array
#include<stdio.h>
int main(void)
{
int arr[5]={1,2,3,4,5};
int (*ptr)[5]=&arr;
printf("ptr=%p\n",ptr); i am not getting the diff btw both statements
printf("*ptr=%p\n",*ptr);...
-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 ...
3
votes
0
answers
105
views
GDB misses to display const qualifier?
Give the following source (main.c):
void foo(const char (*pa)[4])
{
}
int main(void)
{
const char a[4] = "bar";
foo(&a);
}
... compiled with GCC (gcc (Debian 4.9.2-10) 4.9.2) and run under ...
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 ...
0
votes
2
answers
2k
views
Pointer to pointer Array
I am trying to make an 'int** arr[5]' the each cell in it contains an 'int* array', each 'int* array' has a different size. Whenever i am trying to print one of the cell it prints only the first ...
3
votes
1
answer
601
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 ...
2
votes
0
answers
94
views
Array of Pointers to 2d Arrays
I have 5 different 2d Arrays in contrast to the normal usage of 2d Arrays.
int A[1][2] = {2, 5};
int B[1][2] = {6, 1};
int C[1][2] = {4, 8};
int D[1][2] = {3, 6};
int E[1][2] = {9, 7};
Then I have ...
0
votes
1
answer
199
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
1
answer
220
views
Function with a pointer to an array argument in C
I have written code below that contains a function with a pointer to a double array of size 3.My problems is this:
when I pass the address of pointer to a double variable (that clearly isn't an array) ...
1
vote
2
answers
59
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) ...
-1
votes
1
answer
154
views
How to initialize an array inside a structure?
I have a structure defined as
struct new{
int x;
int y;
unsigned char *array;
};
where I want array to be an array which is initialized dynamically based on user input. Inside main function:
...
2
votes
6
answers
102
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 ;
...
0
votes
2
answers
101
views
how do i access an array returned as a pointer from a function using a subscript?
I have created a function that returns a pointer to an array of strings. The function should traverse a linked list and it should assign the data from each node into an array of string. Here is my ...
3
votes
1
answer
342
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) ...
-5
votes
7
answers
92
views
What will be the value in the below cases?
If I define an array arr[] and define a pointer to it:
int *p=arr;
If the value of arr (in terms of address) is 0x1234. What will be the value of p? Will it be the same?
2
votes
1
answer
160
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
votes
2
answers
115
views
vector of Objects having pointer of Object
Quick question guys, I have the following issue:
I have an Object (struct) pointer in my code, and when I modify something, to keep track of its history, I'm saving it in a vector (stack) of Objects.
...
1
vote
4
answers
108
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,...
2
votes
3
answers
228
views
Why should we use pointer-to-arrays in C/C++?
#include<stdio.h>
#define SIZE 3
int main()
{
char intArrayOne[SIZE] = {'A', 'B', 'C'};
char (*ptrToAnOneDimArray)[SIZE] = &intArrayOne;
int i = 0;
for(i=0 ; i<SIZE ...
0
votes
0
answers
34
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?...
4
votes
3
answers
246
views
If int (*p_arr)[10] is defined as a pointer to an array of size 10 then why compiler shows warning in this case?
I have this code:
#include <stdio.h>
int main()
{
int arr[10] = {0};
int *p1_arr = arr;
int (*p2_arr)[10] = arr; // Line 7, Shows Warning here
...
return 0;
}
On ...