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
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 4 years ago.
Improve this question
void Remove(int x)//x is the number that i want to remove
{
for(int i=0;i<CAPACITY;i++)//loop is to find the first case of x
{
if(x==data[i])//if x is in data
{
cout<<data[i]<<endl;//for debugging
data[i]==0; //change x to 0
cout<<data[i]<<endl;
}
}
}
when i cout to see if it works the number that i wanted to delete is still there.
Here is the output before i run it when x=15:
12,15,20,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
I used cout to see if there was a problem with the condition however it runs if x is in the array.
Here is the output after, even if x is in the array:
12,15,20,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
In your If loop you use comparison operator '==' so put only one = which means you assign variable x to data array.
The problem in in the line data[i]==0; //change x to 0
== is an comparison operator. In order to assign a value use = instead. So:
data[i] = 0
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
I'm new to C++ and I don't understand why I'm getting a not declared error on this:
int main(){
string listOfColors[5] = {"red","blue","green","yellow","magenta"};
for(int i = 0;i < sizeof listofColors;i++){
cout << listofColors[i] << "\n";
}
return 0;
}
This is my first utilization of an array so far, so I may just not be declaring it correctly. I also had the array declaration before the main function beforehand.
You declared your variable as listOfColors (capital "O"), and then you use it as listofColors in your for loop. All you need to do is to capitalize the "O" when using your variable.