I don't understand initialization of pointer - pointers

int main ()
{
int num_1 = 111;
int *p = &num_1;
int &ref1 = *p;
int *(&ref2) = p;
printf("&ref2 : %d\n", &ref2);
printf("*ref2 : %d\n", *ref2);
printf("ref2 : %d\n", ref2);
return 0;
}
I get pointer literally points address.
So in int &ref1 = *p; ref1's address is equal to num_1's address, hence have the same value as num1 which is 111.
However, what I don't understand is ref2 part.
If int *(&ref2) is the value of ref2 variable, shouldn't it have the same address of the num_1?

If int *(&ref2) is the value of ref2 variable, shouldn't it have the same address of the num_1?
The declaration int *(&ref2) = p; defines ref2 to be a reference to p.
Part of your misunderstanding may come from wrong output you get from using inappropriate printf conversion specifiers; it's wrong to print addresses with %d - in your first and third printf use %p instead. It might become clearer if you add
printf("p : %p\n", p);

Related

Invalid operands to binary expressions

I was solving N queens problem and I wrote isvalid function, but the function gives "invalid operands to binary expressions" error. The error occurs at board[X][Y] == 'Q':
bool isvalid(vector<vector<string>>& board , int &row , int &col , int &n){
int x = row ;
int y = col ;
while(y >= 0){
if(board[x][y] == 'Q'){// THE ERROR IS OCCURING HERE AT BOARD[X][Y]== 'Q'
return false;
}
y-- ;
}
int x = row ;
int y = col ;
while(x >=0 and y>=0){
if(board[x][y] == 'Q')return false;
x-- ;
y-- ;
}
int x = row ;
int y = col ;
while(x < n and y>=0){
if(board[x][y] == 'Q')return false;
x++ ;
y-- ;
}
return true
}
The problem is that there's no corresponding operator== that could be called for the comparison. board[x][y] is a std::string, whereas 'Q' is a char. As you can see on cppreference, second parameter of operator== for comparison with std::string (which is std::basic_string<char>) can either be another std::string (taken by const reference) or const char* (C-style string). Neither of these is char or could be obtained through implicit conversion from char. In particular, see that there's no constructor of std::string taking a single character.
So, the easiest solution is to compare with a string literal "Q" instead. This is an array object of type const char[2] (second character is a null terminator '\0'), which decays into const char* pointer through array-to-pointer conversion, so appropriate overload of operator can be used:
board[x][y] == "Q"
Also, note that you missed a ; after the return statement.

how to return a pointer in a function returning char *?

I'm implementing my own strrchr - it searches for the last occurrence of the character c (an unsigned char) in the string pointed to by the argument str.
example:
Input: f("abcabc" , "b")
Output: "bc"
the function should return char *. How can i return a pointer to the char array in the function?
#include <stdio.h>
#include <string.h>
char* my_strrchr(char* param_1, char param_2)
{
int len = strlen(param_1);
char res ;
for(int i = len; i >= 0 ;i-- ){
if (param_1[i] == param_2){
res = param_1[i];
return *(res);
//here i tried return (char) res, (char*) res; nothing works
}
}
return NULL;
}
int main(){
char *d = "abcabc";
char r = 'b';
my_strrchr(d, r);
return 0 ;
}
You're trying to return value, not a pointer. Operator * means to get value by a pointer, when res isn't a pointer. You should make it a pointer an then return:
char* my_strrchr(char* param_1, char param_2)
{
int len = strlen(param_1);
char *res ; //make it a pointer
for(int i = len; i >= 0 ;i-- ){
if (param_1[i] == param_2){
res = &param_1[i]; //store address of an element to a pointer
return res; //return a pointer
}
}
return NULL;
}
Your variable res is of type char. To get the reference use the reference operator & (See Meaning of "referencing" and "dereferencing" in C):
return &res
However this will result in the address of your variable res and not in the address within your param_1 array. Have a look at Alex' answer to get the correct reference address: https://stackoverflow.com/a/61930295/6669161

How do I swap two pointer values without using third variable

*a=10
*b=20
How to swap them without using the third variable? Output should be like
*a=20
*b=10
Not sure if the interviewer was looking for XOR over something else but it seems you can simply use +, -, and x. Should work if a is bigger or negative as well.
*a+=*b
*b-=*a
*b=*b x -1
*a-=*b
In your example that would give us:
*a+=*b --> *a = 30
*b-=*a --> *b = -10
*b=*b x -1 --> *b = 10
*a-=*b --> *a = 20
Here is a simple code to do so:
#include <stdio.h>
#include <stdlib.h>
void usingXOR(int** x, int** y){
unsigned long long a = (unsigned long long)*x;
unsigned long long b = (unsigned long long)*y;
a = a^b;
b = a^b;
a = a^b;
*x = (int*)a;
*y = (int*)b;
}
void main(){
int x=5;
int y=10;
int* a = &x;
int* b = &y;
//If you only want to swap the values the pointers are pointing to
//Here the addresses the pointers are holding dont get swapped
(*a) = (*a)+(*b);
(*b) = (*a)-(*b);
(*a) = (*a)-(*b);
//If you want to swap addresses in the pointers
//printf("Before swap address a: %p\n", a);
//printf("Before swap address b: %p\n", b);
//usingXOR(&a,&b);
printf("a: %d\n", *a);
printf("b: %d\n", *b);
//printf("After swap address a: %p\n", a);
//printf("After swap address b: %p\n", b);
}

What's wrong with my dynamic programming solution for uva 10739?

I'm solving this problem on uva. I've found the recurrence relation and it works perfectly for the given test cases. However, without memoization, it exceeds time limit. I cached the values and returned the cache(basic memoization). With caching, I'm getting an answer of 1 more than the actual answer for the last two test cases. I can't understand what might be the bug because it works if you take out the caching. Thanks for your help.
Code:
#include<iostream>
using namespace std;
string a;
int n;
int dp[1005][1005];
int solve(int i, int j, int moves)
{
if(j<=i)
return dp[i][j] = moves;
if(dp[i][j]!=-1)
return dp[i][j];
if(a[i]==a[j])
return dp[i][j] = solve(i+1, j-1, moves);
else
return dp[i][j] = min(min(solve(i+1, j-1, moves+1), solve(i+1, j, moves+1)), solve(i, j-1, moves+1));
}
int main()
{
int T;
cin >> T;
while(T--)
{
cin >> a;
n = a.length();
memset(dp, -1, sizeof(dp));
int ans = solve(0, n-1, 0);
cout << ans << "\n";
}
}
Expected O/P for:
sadrulhabibchowdhury: 8
My Output: 9

What do I need to do before i switch / change a pointer variable pointed-to?

I do not want to mess up my RAM or make problem / bug that related to memory.
So.. what do I need to do before i switch / change a variable pointer pointed-to?
Or.. what i've doing is just fine?
Here is my source code:
#include <stdio.h>
int main(int argc, char *argv[])
{
int x = 10;
int y = 87;
int arr[5] = {1,2,3,4,5};
int *ptr;
ptr = &x;
printf("Now ptr pointed to x --> *ptr = %d ~ ptr address: %p \n", *ptr, ptr);
ptr = &y;
printf("Now ptr pointed to y --> *ptr = %d ~ ptr address: %p \n", *ptr, ptr);
ptr = arr;
printf("1st 2 byte: %d \n", *ptr);
*ptr++;
printf("2nd next 2 byte: %d \n", *ptr);
*ptr++;
printf("3rd next 2 byte: %d \n", *ptr);
*ptr++;
printf("4th next 2 byte: %d \n", *ptr);
// Now i want to switch to x again :D
ptr = &x;
printf("Now ptr pointed to x AGAIN --> *ptr = %d ~ ptr address: %p \n", *ptr, ptr);
return 0;
}
Please enlightenment.
Thank You
There doesn't seem to be a problem with what you have. The pointer is simply changing where it points to, but those background variables are not being changed at all. The variables x, y and your array will be alive for as long as the main function is running, as they are within the scope of main. If you want them to be alive for even less time, you could restrict them to other functions that are called from main.
void xVariable()
{
int x = 7;
}
int main()
{
int y = 8;
xVariable();
for(int i = 0; i < 9; i++)
{
int z = 9;
}
return 0;
}
In this example, y will be alive the entire run of the program. x will only be alive while the function xVariable is running. i and z are only alive for the duration of the loop. This is all a basic example of how variable scope works, but I would recommend looking it further if memory is going to be important.

Resources