Questions tagged [const-pointer]

36 questions
10
votes
1 answer

C++ const double pointer

I want to make a constant double pointer points to a constant pointer points to a constant double. I started to make it (of course I make a little search at books and I googled it) from scratch and think what the following three make: const double*…
10
votes
5 answers

the type of this* in C++

it might sound stupid.in C++prime 5th edition P258,it says: by default, the type of this is a const pointer to the nonconst version of the class type.for example,by default, the type of this in a Sales_data member function is Sales_data *const. i…
ItsJingran
  • 147
  • 9
8
votes
4 answers

How to find by a const pointer key in a map with non-const pointer keys

The following C++ code does not compile because it's passing a non-const pointer to a find() function which expects a const pointer. #include std::map mymap; double myfind(const int * mykey) { return…
bedrorom
  • 415
  • 5
  • 16
6
votes
2 answers

Meaning of const pointers as function parameters

I have this function signature: void myFunction(int *const ptr); What's the point of the const keyword in this particular context? Even if ptr wasn't a const variable, I couldn't modify to what address it points to, because it's passed by value, so…
mariusmmg2
  • 713
  • 18
  • 37
5
votes
3 answers

Why Can't Make a Reference To Pointer for Constant Assign a String Literal

I can make pointer point to string literal, it will cast string literal to constant pointer, but reference to pointer can't assign string literal, for example: const char *&text = "Hello, World\n"; There's an error, the compiler says I can't cast…
user13739935
5
votes
1 answer

Is it a good idea to use a const pointer to FILE type?

Normally, C file I/O is done using FILE* as this is what the standard library functions all take. In C, one can also have a const pointer, where the address of the pointer cannot change (but the value it points to can be). I wondered if this could…
saxbophone
  • 779
  • 1
  • 6
  • 22
4
votes
3 answers

Dynamically allocating memory for const char string using malloc()

I am writing a program that reads a value from an .ini file, then passes the value into a function that accepts a PCSTR (i.e. const char *). The function is getaddrinfo(). So, I want to write PCSTR ReadFromIni(). To return a constant string, I plan…
paperduck
  • 1,175
  • 1
  • 16
  • 26
4
votes
2 answers

Iterating through C-style array not using a pointer

I am learning pointer arithmetic and have a piece of code giving me error for quite some time. any help would be appreciated.(I couldnt find it on SO) int arr [] = {1, 2, 3, 4, 5} ; for (int i = 0 ; i < 5 ; i++) { cout << *arr ; arr++…
Sasha
  • 492
  • 2
  • 6
  • 21
3
votes
3 answers

Modifying a const pointer in C

I am trying to understand why the following code compiles and runs fine. I would expect any assignment using data inside f not to compile with a gcc error assignment of member ‘i’ in read-only object. Is there some kind of exception, because data.i…
b1000
  • 87
  • 1
  • 7
3
votes
2 answers

Prefer Iterators Over Pointers?

This question is a bump of a question that had a comment here but was deleted as part of the bump. For those of you who can't see deleted posts, the comment was on my use of const char*s instead of string::const_iterators in this answer: "Iterators…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
2
votes
1 answer

function returning const pointer

#include class Test { public: int* i = nullptr; int* const X() { return i; } }; int main() { Test c; int x = 2; c.i = c.X(); c.i = &x; *c.i += 2; } what does the const change in the…
user20295136
2
votes
1 answer

casting int * const to long int * const warning

This may well have been asked in some other way before (I would be surprised if its not) but I am struggling to find it if it is. Given: #include #include int main() { int * const pi = new int(1); long int * const pl =…
code_fodder
  • 15,263
  • 17
  • 90
  • 167
2
votes
2 answers

How to initialize int *const *const?

I need a 2d array with fixed width and height that can only change the individual values stored in it. It is declared in a header and later initialized in a source file. What I found made me try the following snippets; unfortunately questions were…
Al.G.
  • 4,327
  • 6
  • 31
  • 56
2
votes
2 answers

c - what does this 2 const mean?

code: const char * const key; There are 2 const in above pointer, I saw things like this the first time. I know the first const makes the value pointed by the pointer immutable, but did the second const make the pointer itself immutable? Anyone can…
Eric
  • 22,183
  • 20
  • 145
  • 196
2
votes
1 answer

How to get CConstPointer in Swift?

I want to transform a CGPath with the transform CGAffineTransformMakeRotation(radians) but the CGPathCreateCopyByTransformingPath func takes a CConstPointer. How do I get a CConstPointer out of my…
JuJoDi
  • 14,627
  • 23
  • 80
  • 126
1
2 3