Reference variables alias in c++ - c++

I am new in coding Please explain this code. I didn't get this logic.
#include<iostream>
void main()
{
int a = 32, *p = &a;
char c ='A', &ch = c;
ch += a;
*p += c;
cout << "\n" << a << " " << c << endl;
}

Since p is the location of a, *p – the int at that location – is the same int as a.
Similarly, since ch is a reference to c, ch is the same char as c.
Thus, what you have is equivalent to (after fixing the typos)
#include<iostream>
int main()
{
int a = 32;
char c ='A';
c += a;
a += c;
std::cout << "\n" << a << " " << c << std::endl;
}

Related

Dealing with Pointers in C++

In the following code, I am having trouble figuring out how to make b point to c so that if you change the value pointed to by b, c is changed (but not a). I tried int *b = &c;. So my question ultimately is: How do you point to a value that is pointing to another, without changing the value of the former?
#include <iostream>
int main(){
int a = 8;
int c = 12;
//std::cout<< a<< std::endl;
std::cout << &a << std::endl;
int *b = &a;
std::cout << *b << std::endl;
std::cout << b << std::endl;
std::cout << b+1 << std::endl;
a = 6;
std::cout << *b << std::endl;
b = &c; // just edited this now it works.
//std::cout << c << std::endl;
system("pause");
return 0;
}
Just take the address of c and assign it to b:
b = &c;
Now if you dereference b, with *b, you get the object denoted by c. If you modify this object in anyway, it will not affect the value of a.

Using pointers in struct to change data

I have a program with one structnamed sample, it contains 2 int members and one char *. when creating 2 objects called a and b, I try assign a new dynamic string to a with the pointer and then copy all the values to b. so b = a. But later on when try to make changes to a like this : a.ptr[1] = 'X'; the pointer in b also changes. I want to know why, and how can I solve this.
struct Sample{
int one;
int two;
char* sPtr = nullptr;
};
int _tmain(int argc, _TCHAR* argv[])
{
Sample a;
Sample b;
char *s = "Hello, World";
a.sPtr = new char[strlen(s) + 1];
strcpy_s(a.sPtr, strlen(s) + 1, s);
a.one = 1;
a.two = 2;
b.one = b.two = 9999;
b = a;
cout << "After assigning a to b:" << endl;
cout << "b=(" << b.one << "," << b.two << "," << b.sPtr << ")" << endl << endl;
a.sPtr[1] = 'X' ;
cout << "After changing sPtr[1] with 'x', b also changed value : " << endl;
cout << "a=(" << a.one << "," << a.two << "," << a.sPtr << ")" << endl;
cout << "b=(" << b.one << "," << b.two << "," << b.sPtr << ")" << endl;
cout << endl << "testing adresses for a and b: " << &a.sPtr << " & b is: " << &b.sPtr << endl;
return 0;
}
Your struct contains a char*. When you assign all values in a to b, the pointer is also copied.
This means that a and b now point to the same char array. Therefore changing a value in this char array changes it for both structs.
If you do not want this, make a new char array for b and use strcpy.
You are copying the pointer not the value. To solve this you could override your assignment operator in the structure:
struct Sample{
int one;
int two;
char* sPtr = nullptr;
Sample& operator=(const Sample& inputSample)
{
one = inputSample.one;
two = inputSample.two;
sPtr = new char[strlen(inputSample.sPtr) + 1];
strcpy (sPtr, inputSample.sPtr);
return *this;
}
};

Writing value to c style string in struct

For the life of me I can't figure out why the I can't write to a c style string inside of a struct.
College student - can't use string class, haven't learned pointers.
Help? 2 hours at trying to figure this out.
#include <iostream>
using namespace std;
void strCopy(char from[], char to[])
{
for (int i = 0; i < 255; i++)
{
to[i] = from[i];
}
}
struct card
{
char suit[20];
char rank[20];
int cvalue;
char location[20];
};
void printCard(card card)
{
cout << card.rank << " of " << card.suit << endl;
}
int main()
{
// I don't think strCopy()'s the problem, I've used it with my last project.
cout << "Test strCopy()" << endl;
char str1[14] = "abcdefghijklm";
char str2[14];
strCopy(str1, str2);
cout << " " << str2 << endl << endl;
// Now the negative.
card one;
one.cvalue = 2;
strCopy("Somewhere", one.location);
strCopy("Two", one.rank);
strCopy("Hearts", one.suit);
printCard(one);
}
// I don't think strCopy()'s the problem, I've used it with my last
project.
Wrong
for (int i = 0; i < 255; i++)
{
to[i] = from[i];
}
copies 255 characters, however that's not what you meant.
If here :
strCopy(str1, str2);
cout << " " << str2 << endl << endl;
Your're getting "correct" output, then you're just unlucky, since that invokes an undefined behavior, an you're writing off the end of the array.

strip characters from char array pass as pointer in C++

any suggestion on how to strip characters from char array pass as pointer in C++. i must use memcpy function to copy.
void foo(char *test)
{
char a[1] = {0};
char b[1] = {0};
char c[1]= {0};
memcpy(&a,&test[0],1);
memcpy(&b,&test[1],1);
memcpy(&c,&test[2],1);
cout << a <<endl;
cout << b <<endl;
cout << c <<endl;
}
int main()
{
char uibuffer[4] = "ABC";
foo(uibuffer);
return 0;
}
the current output is:
ABC��
BC��
C��
desired output is:
A
B
C
void foo(char *test)
{
/* Please note, you need to add one extra byte here for a terminator */
char a[2] = {0};
char b[2] = {0};
char c[2]= {0};
memcpy(&a,&test[0],1);
memcpy(&b,&test[1],1);
memcpy(&c,&test[2],1);
cout << a <<endl;
cout << b <<endl;
cout << c <<endl;
}
int main()
{
char uibuffer[4] = "ABC";
foo(uibuffer);
return 0;
}
OR
Think about improving your code by getting rid of arrays and memory copying. Simple char a = buffer[x] will do the trick.
since you haven't created \0 terminated strings do:
cout << a[0] <<endl;
cout << b[0] <<endl;
cout << c[0] <<endl;
Don't make a, b and c an array. Probably won't compile but to illustrate.
void foo(char *test)
{
char a = 0;
char b = 0;
char c = 0;
memcpy(&a,&test[0],1);
memcpy(&b,&test[1],1);
memcpy(&c,&test[2],1);
cout << a <<endl;
cout << b <<endl;
cout << c <<endl;
}
int main()
{
char uibuffer[4] = "ABC";
foo(uibuffer);
return 0;
}
Don't make them arrays:
char a = test[0];
char b = test[1];
char c = test[2];
You can still use memcpy, if you must, just as you are now.

Prefix increment in C++ preprocessor

Could somebody explain why b = 150 ?
#define CUBE(X) ((X) * (X) * (X))
using namespace std;
int main( void )
{
int a = 3,b = 0;
cout << "before "<< endl;
cout << "a = " << a;
cout << endl;
cout << "b = " << b;
cout << endl;
cout << "after"<< endl;
b = CUBE( ++a );
cout << "a = " << a;
cout << endl;
cout << "b = " << b;
getchar();
return 0;
}
Because you're using a macro. Macros are not functions.
The line:
b = CUBE( ++a );
gets re-written as:
b = ((++a) * (++a) * (++a))
before your code compiles.
The code then invokes Undefined Behaviour because you increment a several times between sequence points.
It would be better if you used a function instead.
(++a) * (++a) * (++a) is undefined behaviour.
Undefined behaviour- you modify a more than once between sequence points. This is why inline functions are the vastly superior option.