-
June 21st, 2011, 06:29 PM
#1
re-initializing an array
Hi
Please have a look on the embedded questions in the code below. Thank you for all the help.
Code:
// learning initialization.cpp
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
const int C = 20;
int main()
{
char name[C] = {0}; // 10 zeroes
int v = 10;
cout << v << endl;
v = 20;
cout << v << endl;
cout << name << endl;
name[C] = {2} /* is this possible to re-initialize an array this way? one two and 9 zeroes */
system("pause");
return 0;
}
-
June 21st, 2011, 07:13 PM
#2
Re: re-initializing an array
Originally Posted by heights
name[C] = {2} /* is this possible to re-initialize an array this way? one two and 9 zeroes */
No.
The term initialization has a specific meaning in C++. Nothing in C++ can be initialized more than once. Once something is initialized, it can only be changed using assignment.
Regards,
Paul McKenzie
-
June 21st, 2011, 10:02 PM
#3
Re: re-initializing an array
If you want you can use scoping rules to make one version of the variable go out of scope, and then bring another one in:
Code:
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
const int C = 20;
int main()
{
{
char name[C] = {0}; // 10 zeroes
int v = 10;
cout << v << endl;
v = 20;
cout << v << endl;
cout << name << endl;
}
{
char name[C] = {2};
system("pause");
return 0;
}
}
Note these are completely separate arrays which just happen to have the same name.
-
June 22nd, 2011, 05:48 AM
#4
Re: re-initializing an array
Code:
char name[C] = {0}; // 10 zeroes
//
name[C] = {2} /* is this possible to re-initialize an array this way? one two and 9 zeroes */
1. The second statement is only legal in C++0x
2. The way that I read the standard ... the second statement is the same as :
(which is out of bounds of the array).
Last edited by Philip Nicoletti; June 22nd, 2011 at 05:52 AM.
-
June 23rd, 2011, 06:40 AM
#5
Re: re-initializing an array
Originally Posted by heights
Hi
Please have a look on the embedded questions in the code below. Thank you for all the help.
Code:
// learning initialization.cpp
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
const int C = 20;
int main()
{
char name[C] = {0}; // 10 zeroes
int v = 10;
cout << v << endl;
v = 20;
cout << v << endl;
cout << name << endl;
name[C] = {2} /* is this possible to re-initialize an array this way? one two and 9 zeroes */
system("pause");
return 0;
}
It has many 3 errors
Code:
Compiling...
main.cpp
..............cpp(22) : error C2059: syntax error : '{'
..............cpp(22) : error C2143: syntax error : missing ';' before '{'
..............cpp(22) : error C2143: syntax error : missing ';' before '}'
in line
Code:
name[C] = {2} /* is this possible to re-initialize an array this way? one two and 9 zeroes */
you are trying to access 20th data of name array but is that exist. NO. Your program may crach in such condition.
I like the answer of paul he says about initialization meaning.
Originally Posted by Paul McKenzie
The term initialization has a specific meaning in C++. Nothing in C++ can be initialized more than once. Once something is initialized, it can only be changed using assignment.
heights you are understanding wrong meaning of initialization.
If you want to change all the data in array to 2 then try this code
Code:
// learning initialization.cpp
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
const int C = 20;
int main()
{
char name[C] = {0}; // 10 zeroes
int v = 10;
cout << v << endl;
v = 20;
cout << v << endl;
cout << name << endl;
for(int i=0;i<20;i++)
name[i]='2';
return 0;
}
one basic question to you in red region i put name[i]='2' not name[i]=2 why. what will change if i do so.
Last edited by vkash; June 23rd, 2011 at 06:46 AM.
Reason: add question
-
June 23rd, 2011, 11:34 AM
#6
Re: re-initializing an array
Originally Posted by vkash
heights you are understanding wrong meaning of initialization.
If you want to change all the data in array to 2 then try this code
But what the OP understood correctly and you did not, is that the code
Code:
char name[10] = {2};
does not initialize the array with ten 2s.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
June 23rd, 2011, 11:52 AM
#7
Re: re-initializing an array
Originally Posted by vkash
It has many 3 errors
Code:
Compiling...
main.cpp
..............cpp(22) : error C2059: syntax error : '{'
..............cpp(22) : error C2143: syntax error : missing ';' before '{'
..............cpp(22) : error C2143: syntax error : missing ';' before '}'
in line
Code:
name[C] = {2} /* is this possible to re-initialize an array this way? one two and 9 zeroes */
you are trying to access 20th data of name array but is that exist. NO. Your program may crach in such condition.
I like the answer of paul he says about initialization meaning.
heights you are understanding wrong meaning of initialization.
If you want to change all the data in array to 2 then try this code
Code:
// learning initialization.cpp
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
const int C = 20;
int main()
{
char name[C] = {0}; // 10 zeroes
int v = 10;
cout << v << endl;
v = 20;
cout << v << endl;
cout << name << endl;
for(int i=0;i<20;i++)
name[i]='2';
return 0;
}
one basic question to you in red region i put name[i]='2' not name[i]=2 why. what will change if i do so.
It's great that you're trying to help, but by the questions you ask, I'm not sure you're ready. You may want to look at this table to find out the difference between the character 2 and the numeric value of 2.
http://www.asciitable.com/
Also, name[C] is the 21st element of name, not the 20th.
Last edited by GCDEF; June 23rd, 2011 at 12:04 PM.
-
June 23rd, 2011, 12:33 PM
#8
Re: re-initializing an array
Originally Posted by GCDEF
It's great that you're trying to help, but by the questions you ask, I'm not sure you're ready. You may want to look at this table to find out the difference between the character 2 and the numeric value of 2.
http://www.asciitable.com/
Also, name[C] is the 21st element of name, not the 20th.
GCDEF when i start my C++ i have learnt these things carefully and i know these things still now also.I ask his question to heights because i want to tell him are you perfect in this thing.
My answer to my own question is
when we write
Code:
for(int i=0;i<20;i++)
name[i]='2'
here name [i] is set to 2 but when we type
Code:
for(int i=0;i<20;i++)
name[i]=2
here we are telling to set name[i] to that value whose number in ASCII standards is 2. there are 256 such characters. these characters repeat after 256(-128 to 128). I have got this info from my VC++ code that is
Code:
#include<iostream>
using namespace std;
int main()
{
for(int i=-150;i<128;i++)
cout<<i<<" "<<char(i)<<endl;
return 0;
}
you have good link but i had already got all those things.
Originally Posted by vkash
you are trying to access 20th data of name array but is that exist
Originally Posted by GCDEF
Also, name[C] is the 21st element of name, not the 20th.
heights i am sorry for wrong answer.
shiiiit i go here in bjarne stroustrup method of counting not general counting method. I don't know why bjarne stroustrup break convention to start counting from 0 rather than 1. there may be several reasons like first ten have single digit in such counting method.
Last edited by vkash; June 23rd, 2011 at 12:39 PM.
-
June 23rd, 2011, 01:04 PM
#9
Re: re-initializing an array
Originally Posted by vkash
GCDEF when i start my C++ i have learnt these things carefully and i know these things still now also.I ask his question to heights because i want to tell him are you perfect in this thing.
My answer to my own question is
when we write
Code:
for(int i=0;i<20;i++)
name[i]='2'
here name [i] is set to 2 but when we type
Code:
for(int i=0;i<20;i++)
name[i]=2
here we are telling to set name[i] to that value whose number in ASCII standards is 2. there are 256 such characters. these characters repeat after 256(-128 to 128). I have got this info from my VC++ code that is
Code:
#include<iostream>
using namespace std;
int main()
{
for(int i=-150;i<128;i++)
cout<<i<<" "<<char(i)<<endl;
return 0;
}
you have good link but i had already got all those things.
heights i am sorry for wrong answer.
****i go here in bjarne stroustrup method of counting not general counting method. I don't know why bjarne stroustrup break convention to start counting from 0 rather than 1. there may be several reasons like first ten have single digit in such counting method.
Oh dear. We don't use profanity in this forum.
Stroustrup has nothing to do with array indexes beginning with 0. Think of the index as an offset from the start of the array, not an index number.
You still seem a little confused between the difference between the number 2 and the character 2.
-
June 23rd, 2011, 03:06 PM
#10
Re: re-initializing an array
Originally Posted by GCDEF
Oh dear. We don't use profanity in this forum.
.
I have no intention of abusing(profanity) here.
I don't abuse in real life as well as in any public internet place like this this. I write this word here because i think it's meaning is different. now i know that it is profanity in modern English.
Originally Posted by GCDEF
You still seem a little confused between the difference between the number 2 and the character 2
I don't thing i am confused between these number2 and character 2.
if you think so then explain it in your words. heights will also get benefit from from this.
Last edited by vkash; June 23rd, 2011 at 03:09 PM.
-
June 23rd, 2011, 03:13 PM
#11
Re: re-initializing an array
Originally Posted by vkash
My answer to my own question is
when we write
Code:
for(int i=0;i<20;i++)
name[i]='2'
here name [i] is set to 2 but when we type
Code:
for(int i=0;i<20;i++)
name[i]=2
here we are telling to set name[i] to that value whose number in ASCII standards is 2. there are 256 such characters. these characters repeat after 256(-128 to 128).
Here, 'nSomeInt' is not the value 2, it is 50.
Here, 'nSomeInt' is the value 2.
This is where you appear confused.
Viggy
Last edited by MrViggy; June 23rd, 2011 at 03:16 PM.
-
June 23rd, 2011, 03:16 PM
#12
Re: re-initializing an array
Originally Posted by vkash
I don't know why bjarne stroustrup break convention to start counting from 0 rather than 1.
C++ has its roots in the 'C' language, and in 'C', array indices start from 0. The inventors of the C language are Brian Kernighan and Dennis Ritchie, not Bjarne Stroustrup.
Regards,
Paul McKenzie
-
June 23rd, 2011, 03:23 PM
#13
Re: re-initializing an array
Originally Posted by Paul McKenzie
C++ has its roots in the 'C' language, and in 'C', array indices start from 0. The inventors of the C language are Brian Kernighan and Dennis Ritchie, not Bjarne Stroustrup.
Regards,
Paul McKenzie
OK.
Originally Posted by MrViggy
.
I am not confused. It is the way of my writing that giving you impression that i am confused. My bad knowledge of English is also responsible for this.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|