pointers

Dereferencing a pointer to an array?

我们两清 提交于 2021-02-06 09:12:10
问题 Referring to the line with the comment: Why does adding parenthesis in the example work to print all the contents of the array? The example prints "one", then prints garbage. #include <iostream> int main() { const char* a[3] = { "one", "two", "three" }; const char*(*p)[3] = &a; for(int i = 0; i < 3; i++) { std::cout << *p[i] << std::endl; // this line } return 0; } It works after changing to this: std::cout << (*p)[i] << std::endl; 回答1: p is a pointer to an array of 3 elements like this: ┌───

Can a pointer be volatile?

僤鯓⒐⒋嵵緔 提交于 2021-02-06 07:56:08
问题 Consider the following code: int square(volatile int *p) { return *p * *p; } Now, the volatile keyword indicates that the value in a memory location can be altered in ways unknown to the compiler or have other unknown side effects (e.g. modification via a signal interrupt, hardware register, or memory mapped I/O) even though nothing in the program code modifies the contents. So what exactly happens when we declare a pointer as volatile? Will the above mentioned code always work, or is it any

printf() no format string printing character and integer arrays --> garbage

∥☆過路亽.° 提交于 2021-02-05 12:35:20
问题 I'm wondering why printf() when provided an array and no formatting options, successfully prints character arrays but while using integer arrays the compiler throws a warning and a garbage value is printed. Here's my code: #include <stdio.h> int main() { char cr[3] = { 'a', 'b' }; int ar[3] = { 1, 2 }; printf("character array output using printf() : "); printf(cr); printf("\n\nInteger array output using printf() : "); printf(ar); printf("\n"); return 0; } and here's my output: ../main.c: In

Differences between pointer declaration [duplicate]

╄→гoц情女王★ 提交于 2021-02-05 12:29:55
问题 This question already has an answer here : Difference between char* var; and char *var;? [duplicate] (1 answer) Closed 7 years ago . Is there any differences between these two declarations? int* a; int *a; Or these two declarations are the same (pointer to an integer)? 回答1: They're exactly the same, but here's a small gotcha I came across when first learning C years ago. The * binds to the variable , not the type. This means that int* a, b; Declares a as a pointer to int, and b as an int . To

Pointer Confusion: swap method in c

半世苍凉 提交于 2021-02-05 12:29:34
问题 #include<stdio.h> void swap(int *a,int *b){ int p=*b; *b=*a; *a=p; /*int *p=b; b=a; a=p; */ } int main(){ int a,b; scanf("%d %d",&a,&b); swap(&a,&b); printf("%d %d",a,b); } Above is the code. If I put 3 5 as an input, then it should swap its values, and 5 3 should come out as an output. I got my answer by trying int p=*b thing However I also tried commented part, but it didn't work. So, I checked their address in swap and in main. In swap int *a and int *b their address changed However, when

sizeof on argument

守給你的承諾、 提交于 2021-02-05 12:17:37
问题 Even with int foo(char str[]); which will take in an array initialized to a string literal sizeof doesn't work. I was asked to do something like strlen and the approach I want to take is to use sizeof on the whole string then subtract accordingly depending on a certain uncommon token. Cuts some operations than simply counting through everything. So yea, I tried using the dereferencing operator on the array(and pointer too, tried it) but I end up getting only the first array element. How can I

virtual insertion operator overloading for base and derived class

我的未来我决定 提交于 2021-02-05 11:34:07
问题 Can someone please explain how to ensure that the derived function is called from a pointer of type base to a derived object instead of the base function... Also, are the virtual and override keywords best practice to accomplish this? I had previously defined each overload with keyword friend in each class; but the base function was called for the base pointer to derived object. int main() { // contrived example ... base* ptr_derived = new derived(); std::cout << *ptr_derived; delete ptr

What is the difference between Pointer and strings?

会有一股神秘感。 提交于 2021-02-05 11:32:07
问题 What is the difference between a pointer and array or they are same? As a array also works with poiter arithematic so it can be said that an array is nothing but pointer to its fitst element. 回答1: They both are different by the following differences:- int array[40]; int * arrayp; Now if you will try to see the size of both then it will be different for pointer it will same everytime whereas for array it varies with your array size sizeof(array);\\Output 80 sizeof(arrayp);\\Output 4(on 32-bit

Passing in C++ Method as a Function Pointer?

て烟熏妆下的殇ゞ 提交于 2021-02-05 11:14:33
问题 I've created a class called InputControl. I am using a library called GLFW, and a function glfwSetKeyCallback. The function is defined as: GLFWkeyfun glfwSetKeyCallback ( GLFWwindow * window, GLFWkeyfun cbfun ) Typedef is as follows: typedef void(* GLFWkeyfun) (GLFWwindow *, int, int, int, int) The issue is that I can not convert my method to a function pointer. I feel like I've tried to cast it in every way possible. I'm not sure how else I need to cast this method. Is there something that

C++ Returning Array, data loss [duplicate]

醉酒当歌 提交于 2021-02-05 11:01:07
问题 This question already has answers here : Can a local variable's memory be accessed outside its scope? (20 answers) Closed 3 years ago . I'm new to C++ coming from Java so forgive me for being naive, but I'm trying to pass an array out of a function. I know the way to do this is to pass a pointer, like so: int *foo(){ int arr[3] = {1, 2, 3}; int *arrptr = arr; return arrptr; } And then to access that array: int main(){ int *arrptr = foo(); //create a pointer to array //cout all elements for