Questions tagged [array-pointer]

18 questions
2
votes
0 answers

Why does a php array in two foreach loops lead to an infinite loop?

I have this PHP code: $array = [1, 2, 3]; $counter = 0; foreach($array as &$elem){ test(); $counter++; if($counter >= 10){ return; } } function test(){ global $array; foreach($array as $key => $element){ …
Anorionil
  • 505
  • 2
  • 7
  • 17
2
votes
2 answers

How to read a complicated type?

I know that using type alias and typedef make the code so readable, less error-prone and easy modify. However in this example I want to know this complicated type: #include #include char ( * (* x() )[] )(); int main(){ …
Itachi Uchiwa
  • 3,044
  • 12
  • 26
2
votes
2 answers

C Pointer array is not working with memcpy

I would like to concatenate two string with memcpy. But next memcpy is not working. My expected output is "my name is khan". #include #include #include int main() { char *var1 = "my name"; char *var2= "is…
Hemjal
  • 130
  • 2
  • 7
1
vote
2 answers

C26485 and pointer decay with TCHAR in exception handler

I don't understand this C26485 warning I receive. My code is an exception handler: catch (CDBException* e) { TCHAR szError[_MAX_PATH]; e->GetErrorMessage(szError, _MAX_PATH); AfxMessageBox(szError); } It is saying: Expression szError:…
Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
1
vote
0 answers

What is the difference between these two methods of dynamic allocation of arrays?

I was going through array using pointers and I found two different dynamic allocation methods of an array int *ptr_arr=new int[10]; This one was defined in the lectures but when I explored a bit I found a slightly different method: int…
Priyanshu
  • 11
  • 1
1
vote
1 answer

What is the implicit decay of char* arrays?

As a newbie, I'm solving a problem from K&R's pointers chapter and was confused with some aspects of character pointers, and passing an array of type char* as a function parameter. Char array decay I understand Ex: void main( ) { …
UKS
  • 13
  • 2
1
vote
1 answer

Seg Fault calling mergesort on pointer array

I am trying to work with threads in C. I need one of my threads to work on one half of a whole array, and mergesort its half. To do this I created a global array pointer for the whole array and both halves. The is allocated at runtime with…
1
vote
3 answers

Understanding and manipulating pointers to integer arrays

I am having troubles with the following code: int a[] = {1, 2, 3, 4}; fprintf(stdout, "a : %lu\n" "*a : %d\n" "&a : %ld\n" "**(&a) : %d\n", (unsigned long int)a, *a, (unsigned…
anbocode
  • 53
  • 6
0
votes
0 answers

Address of the array name pointer and the first element of the array

Is the address of the first element of the array and the address of the array name pointer is same in C programming? And what if we create a pointer(p) to the first element(a[0]) of the array(a[5]) then the address of the pointer(p) , first…
0
votes
1 answer

value of array of pointers to struct changes when exiting loop

phonebook is an array of pointers. inside the handleMenu im allocating memory and pointing to structure where a first name is assigned to a struct that is pointed from phonebook[11]. as you can see in line 54, the name is assigned correctly and the…
olmervii
  • 17
  • 3
0
votes
0 answers

Accessing specific objects using pointer to multiple arrays of pointers

EDIT: Pasting portion of code that is giving me this issue. Although in my effort to cleanup some of the comments before pasting it here I ended up with a different error. Still happens in the same place though: "munmap_chunk(): invalid…
0
votes
1 answer

Meaning of 1[pointer] in C++?

Reading cppreference I found this example: int a[4] = {1, 2, 3, 4}; int* p = &a[2]; std::cout << p[1] << p[-1] << 1[p] << (-1)[p] << '\n'; // 4242 I am confused about the meaning of 1[p] and (-1)[p]. Asking for help elsewhere I was told: "1[ptr] is…
yrom1
  • 27
  • 1
  • 1
  • 5
0
votes
2 answers

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 arithmetic? Consider this example: int (*foo)[ 10 ];…
Jackson Allan
  • 727
  • 3
  • 11
0
votes
1 answer

confilicting types in C

i have this main and 2 more functions each on different file /*decleration of the functions on one file*/ typedef double *mat[4][4]; void catcher(void *,mat *); void findMat(mat *,char *,int *); int main() { mat MAT_A={0}; mat MAT_B={0}; mat…
Roy Shiff
  • 63
  • 7
0
votes
1 answer

Is there a way to get the size of a three-dimensional vector using pointer notation?

I need to do something like this: sizeof(int[width][height][8]) but unfortunately in c89 (the standard i have to use) this notation is forbidden, is there any way to do the same thing but using pointer notation? Here's the error i get It works fine…
Enrico
  • 84
  • 1
  • 9
1
2