Why two different outputs? [duplicate] - c++

This question already has answers here:
Why does cout print char arrays differently from other arrays?
(4 answers)
Closed 5 years ago.
I am novice in programming. I have written a program and confused in concepts of pointers.
#include <bits/stdc++.h>
using namespace std;
int main()
{
char c[]="hello";
char *a=c;
cout<<a<<endl;
int arr[]={1,2,3,5};
int *p=arr;
cout<<p<<endl;
return 0;
}
When I print a, it prints hello but when I print p it print the address. Why?

std::ostream has overload for const char* to display C-string.
int* would use the void* one which print the address.

Related

Using & with string class objects in c++ [duplicate]

This question already has answers here:
displaying address of char variable in c++ using pointers?
(1 answer)
Why does streaming a char pointer to cout not print an address?
(4 answers)
Closed 9 months ago.
As far as my knowledge in c++, the & character can act as an address of operator(finding the address of a variable in memory) or as a bitwise AND operator or declaring references.
However if I run this code:
#include<iostream>
#include<string>
using namespace std;
int main() {
string s = "Stackoverflow";
cout<<&s[0]<<endl<<&s[1]<<endl;
return 0;
}
Output is
Stackoverflow
tackoverflow
I expected it to print the addresses of the first 2 characters of the string, however, I got the string itself starting from a different index. How does it work?

How can we declare short int as the return type in main function of C++? [duplicate]

This question already has answers here:
What should main() return in C and C++?
(19 answers)
Return type of main function [duplicate]
(5 answers)
Closed 11 months ago.
I am always returning a value of 0 from the main function. I was wondering if we could change the return type of the C++ main method to short int.
#include <iostream>
using namespace std;
short int main() {
cout << "Hello World" << endl;
return 0;
}
Something like this, but the above code returns an error saying that the return type should be 'int'. Can someone please guide me?

using scanf_s to write into a const char* memory [duplicate]

This question already has answers here:
Undefined, unspecified and implementation-defined behavior
(9 answers)
What is the difference between char s[] and char *s?
(14 answers)
Why do I get a segmentation fault when writing to a "char *s" initialized with a string literal, but not "char s[]"?
(19 answers)
Closed 1 year ago.
I have a program as shown below :
This one works fine why? isn't it supposed to be constant for each of the characters? why am I able to overwrite it?
input: "Test" (Any String that is shorter dan 8 last byte reserved for null-terminated)
#include <iostream>
using namespace std;
int main()
{
const char itemss[8] = "1234567";
const char* item = items;
scanf_s("%s", item, 8);
printf(item);
return 0;
}
but when I try to change the code to this :
this one doesn't work, the code is the same as the above code but this one cause segmentation fault, string literal is const char[] (in this case const char[8]) so it should be no different than the code above right? my guess is this code below allocated in read-only memory but then again the code above also use const.
#include <iostream>
using namespace std;
int main()
{
const char* item = "1234567";
scanf_s("%s", item, 8);
printf(item);
return 0;
}

Why sizeof() method is giving different results? [duplicate]

This question already has answers here:
Size of character ('a') in C/C++
(4 answers)
Closed 2 years ago.
When I run the program below in C, I get the output result to be 4.
#include <stdio.h>
//using namespace std;
int main()
{
printf("%d", sizeof('a'));
return 0;
}
But when I run the code below in C++, I get the output result to be 1.
#include <iostream>
using namespace std;
int main()
{
printf("%d", sizeof('a'));
return 0;
}
Could you please explain why do I get different output for the same code as if 'a' is the way we define characters in both the languages ?
In C, a character representation (like 'a') has type int. So, sizeof operator returns the size of an integer.
In C++, it's of a character type.

Why is the size of array not same in both the cases? [duplicate]

This question already has answers here:
Why is sizeof(array) different in these two cases? [duplicate]
(2 answers)
Closed 7 years ago.
#include<iostream>
using namespace std;
void f(int arr[])
{
int a=sizeof(arr);
cout<<a;
}
int main()
{
int n;
cin>>n;
int arr[n];
int a=sizeof(arr);
cout<<a<<"\n";
f(arr);
}
Output:
24
8
Why is my output not same in both the cases even when i am printing the size of same array?
In the first call it shows the size of array.
But in the second case it shows the size of the pointer passed.
When arrays are passed through functions they decay into pointers. So after passing, the:
sizeof(array)
Shows the size of the pointer.