Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
How to scan an array in c++ using pointer?
I have learned c programming .so i tried that way.
include<iostream>
using namespace std;
main()
{
int a[5],*p,i;
p=&a[5];
for(i=0;i<5;i++)
{
cout<<"enter"<<i+1<<endl;
cin>>(p+i);
}
for(i=0;i<5;i++)
{
cout<<*(p+i)<<endl;
}
}
I am expecting to scan using pointer just like in c programming
p=&a[5];
&a[5] is the address to one past the last element of the array. Incrementing this pointer, as well as indirecting through the pointer have undefined behaviour.
What you need is a pointer to the first element. You can use:
p = &a[0]; // this
p = a; // or this
Or you could simply not use p in the first place, and access a[i] directly.
cin>>(p+i);
This is wrong. p+i is a pointer. You cannot extract into an int pointer. You should extract into the integer object instead:
cin >> p[i];
cin >> a[i]; // or without p, as I pointed out above
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 months ago.
Improve this question
CODE1(input):
#include <cstdio>
struct College {
char name[256];
};
void print_name(College* college_ptr) {
printf("%s College\n", college_ptr->name);
}
int main() {
College best_colleges[] = { "Magdalen", "Nuffield", "Kellogg" };
print_name(best_colleges);
}
OUTPUT:
Magdalen College
I have read that "At the slightest provocation, an array will decay into a pointer.A decayed
array loses length information and converts to a pointer to the array’s first element."
For example:
int key_to_the_universe[]{ 3, 6, 9 };
int* key_ptr = key_to_the_universe; // Points to 3
QUESTION1 :
I have also learned how a class and array are initialized but i dont understand how an array in a class can be initialized. According to me array in a class should be initialized ( referring CODE1) this way :
College best_colleges;
best_colleges.name[]={"Magdalen", "Nuffield", "Kellogg"};
QUESTION2:
(Referring CODE1)I also dont understand that if best_colleges array gets converted to pointer college_ptr which points to first element of it i.e
College* college_ptr = &best_colleges[0];
then in function "print_name" , "college_ptr->name" should be equal to "&best_colleges[0]->name". why we need to write "college_ptr-> name" ?
QUESTION 3:
I dont understand if best_colleges is a class or an array according to how it has been initialized.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I am doing my homework and I declared a 2D array but it's not printing what I want.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
char NOTE[7][3] = { {'D','O'} , {'R','E'} , {'M','I'} , {'F','A'} , {'S','O','L'} , {'L','A'} , {'S','I'} };
for (int i = 0; i <7 ; i++)
cout <<setw(10)<< NOTE[i] << "\n";
return 0;
}
I want to get:
DO
RE
MI
FA
SOL
LA
SI
DO
but I got:
DO
RE
MI
FA
SOLLA
LA
SI
DO
NOTE[i] is an array of characters. << NOTE[i] inserts this array into a character stream. The array argument decays to a pointer to the first character.
The documentation says that behaviour is undefined unless the pointer operand points to a null terminated array. The array {'S','O','L'} is not null terminated. Therefore the behaviour of the program is undefined - although it could be argued that the behaviour of iterating across boundaries subarrays of a multidimensional array should be well defined (with the behaviour shown in your program), but strict interpretation of the standard is that doing so is UB.
So, to get the output you want, you either need to 1. null terminate each sub array (and therefore need a larger array), or to 2. print each character individually.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I wanted to assign a variable's address to a pointer by de-referencing it. Why is it giving a segmentation fault? I expected the program to print address of u variable.
#include<iostream>
using namespace std;
int main(){
int t = 4;
int u = 5;
int * p;
*p = &t;
*p = u;
cout << p;
}
Use p = &t; to assign the address to p. p is already a pointer. Or do it on the same line with int *p = &t
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
int b;
printf("hello");
for(b=1;b<=100;++b)
{
if(b%10==1){
cout << "\n";
for(int l=0;l<=100;++l)
cout << "-" ;
cout << endl;
}
printf("|%s|",b);
}
return 0;
}
enter image description here
printf which is placed outside of loop body works fine but the one placed in the loop body of for causes some kind of error while running!! take a look at the picture !
Your b is an int.
You give b where printf() expects a pointer to char and will attempt to dereference the value you give as such.
Since the value you give via b is not a valid pointer to anything, your program has some access problem.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
When running this program, if the value of n is set to 10 it will print the indices of array a and stored values from a[0] to a[9]. However if I set the value of n to more than 10 the then it only prints the indices of array a from a[0] to a[5] with their stored values. Can someone explain to me why this is happening?
#include <iostream>
using namespace std;
int main()
{
int a[10];
int i, n=11;
for(i=0; i<n; i++) {
a[i]= 5;
}
cout<<"The array is: \n";
for(i=0; i<n; i++)
{
cout<<"a["<<i<<"] = "<<a[i]<<endl;
}
return 0;
}
If you increase the value of n past the size of the array (or equal to it indexing from 0), you are going past the end of the array. And going past the end of the array is undefined behavior. If your program is exhibiting undefined behavior. Anything can happen. See this blog post from Microsoft for more on undefined behavior
If you switch to an std::array instead of a C array and use .at(), then something well defined will happen, you will get an std::out_of_range exception. For more see http://en.cppreference.com/w/cpp/container/array/at and http://en.cppreference.com/w/cpp/container/array