Re: [Dev-C++] getting unexpected results
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: <ten...@ne...> - 2003-07-30 16:27:16
|
You've not allocated memory for buf.
You can do one of two options:
A) Declare an statically allocated array, such as char
buf[MAX_STRING_LENGTH_YOU_EXPECT];
or
B) Declarea a pointer to char as you do char *buf; and then allocated
eough memory to it buf = new char[MAX_STRING_LENGTH_YOU_EXPECT]
However, let's make some comments about this program (it's an example
from a book on using pointers?):
After you have assigned each cstr member of each str_ex array member,
all the members will have the same string. Why? Because you input an
string in buf and then you pass its memory location to the cstr member
(a pointer is a variable which value is a memory location). So all
str_ex array member will have their cstr member pointing to the same
memory location, so each time you change the string at buf you are
changing the string at that memory location and being that the same
memory location cstr are pointing to, all them haveit changed to the
same string.
If you want input different strings to each str_ex member array, then
you shall
A) Allocate memory directly to each struct member (str_ex[x].cstr = new
char [MAX_STRING_ETC]; and don't forget free the memory with delete[]
str_ex[x].cstr). This would means this couple of cycles:
for (int i = 0; i < 12; i++) /**of course, you do this cycle if you want
to input the strings for all array members one after the other, if not
you do the line below as many times you need with the valueas as needed**/
str_ex[i].cstr = new char[MAX_ETC];
and before the end of the program
for (int j = 0; j < 12; j++)
delete[] str_ex[j].cstr;
And for each x member array do cin >> str_ex[x].cstr;
B) The other way, which would make you achieve better memory usage
(depending on how many str_ex array size, value of MAX_ETC, and how
they'll be used after input is no more needed) would be using the buf
as intermediate, so you can do something like this.
buf = new char[MAX_ETC];
for (int i = 0; i < 12; i++){ /**Same comment as the previous option**/
cin >> buf;
str_ex[i].cstr = new char[function_you
_define_to_calculate_string_length_including_null_character(buf)];
strcpy(str_ex[i].cstr, buf);
}
So, now each str_ex array member has an cstr member pointing to its own
memory location, which is different for str_ex, and so you can change
them individually.
When you have finished inputting strings
delete[] buf;
then you work with str_ex array.
And it the end you make the same cycle as the previous option.
P.D. - As you gave no information about the expected and unexpected
behaviours, I cover the two options I can think about: that either you
get memory errors (and surely you'll get them) and you expect to change
array members individually and they're changed all them as one.
dawood wrote:
> Can anybody let me know, why the below code is not working correctly.
>
> #include <iostream.h>
> #include <stdlib.h>
>
> int main()
> {
> struct str_misc {
> char* cstr;
> };
> str_misc str_ex[12];
> char* buf;
>
> cin>>buf;
> str_ex[0].cstr= buf;
> cout<<str_ex[0].cstr<<"index=0"<<'\n';
>
> cin>>buf;
> str_ex[1].cstr= buf;
> cout<<str_ex[1].cstr<<"index=1"<<'\n';
>
> cout<<str_ex[0].cstr<<" repeat index=0"<<'\n';
> cout<<str_ex[1].cstr<<" repeat index=1"<<'\n';
>
> system("PAUSE");
> return 0;
> }
>
>
>
> -------------------------------------------------------
> This SF.Net email sponsored by: Free pre-built ASP.NET sites including
> Data Reports, E-commerce, Portals, and Forums are available now.
> Download today and enter to win an XBOX or Visual Studio .NET.
> http://aspnet.click-url.com/go/psa00100003ave/direct;at.aspnet_072303_01/01
>
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|