array values
-
Hi, In the following code,i have assigned a value "test" to c variable. I have assigned the variable c to sz123. If i copied another value to c,the already assigend value in sz123[0] is getting overide. Can anyone please help me how to avoid this one? LPSTR sz123[10]; char c[20] = "test"; sz123[0] = (LPSTR)c; strcpy(c,"123"); thanks,
Well, there can be multiple answers to that, depending on your needs, what tools you have available, what environment you work in e.g:
char sz123\[10\]\[20\]; char c\[20\] = "test"; strcpy(sz123\[0\], c); strcpy(c, "123");
or
std::string sz123\[10\]; char c\[20\] = "test"; sz123\[0\] = c; strcpy(c, "123");
or
CString sz123\[10\]; char c\[20\] = "test"; sz123\[0\] = c; strcpy(c, "123");
or
LPSTR sz123\[10\]; char c\[20\] = "test"; sz123\[0\] = (LPSTR)malloc(sizeof(c\[0\]) \* (strlen(c) + 1)); strcpy(sz123\[0\], c); strcpy(c, "123"); ... free(sz123\[0\]);
> The problem with computers is that they do what you tell them to do and not what you want them to do. < > "It doesn't work, fix it" does not qualify as a bug report. < > Amazing what new features none of the programmers working on the project ever heard of you can learn about when reading what the marketing guys wrote about it. <
-
Hi, In the following code,i have assigned a value "test" to c variable. I have assigned the variable c to sz123. If i copied another value to c,the already assigend value in sz123[0] is getting overide. Can anyone please help me how to avoid this one? LPSTR sz123[10]; char c[20] = "test"; sz123[0] = (LPSTR)c; strcpy(c,"123"); thanks,
you declared array of char pointers sz123 and stored the base adrress of string c to its 0th index. So whenever value in c changes, its reflected in sz123. If you want to store each strings in sz123, allocate memory for each.
LPSTR sz123[10];
char c[20] = "test";
sz123[0] = new char[strlen(c)+1];
strcpy(sz123[0], c);strcpy(c,"123"); //wont affect sz123[0]
-
Hi, In the following code,i have assigned a value "test" to c variable. I have assigned the variable c to sz123. If i copied another value to c,the already assigend value in sz123[0] is getting overide. Can anyone please help me how to avoid this one? LPSTR sz123[10]; char c[20] = "test"; sz123[0] = (LPSTR)c; strcpy(c,"123"); thanks,
Remember you're not really copying values around, you're copying pointers. So when you do:
sz123[ 0 ] = c;
You're just creating an alias to the block of memory that contains "hello." I'd suggest (unless you're stuck programming in C) to avoid using character arrays, character pointers and C style strings and just use C++ strings - then when you copy them around you're actually copying values and not aliases. Cheers, Ash
-
Hi, In the following code,i have assigned a value "test" to c variable. I have assigned the variable c to sz123. If i copied another value to c,the already assigend value in sz123[0] is getting overide. Can anyone please help me how to avoid this one? LPSTR sz123[10]; char c[20] = "test"; sz123[0] = (LPSTR)c; strcpy(c,"123"); thanks,
sz123[0] = _strdup(c);
//...
free(sz123[0]);:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
sz123[0] = _strdup(c);
//...
free(sz123[0]);:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Is it possible to use the globally declared LPSTR variable in other class by using extern function?
Karthika85 wrote:
Is it possible to use the globally declared LPSTR variable in other class by using extern function?
You're mixing a bit different domains here. If the variable is global then you may use it everywhere (don't forget to call
free
when you no longer need the string). :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Karthika85 wrote:
Is it possible to use the globally declared LPSTR variable in other class by using extern function?
You're mixing a bit different domains here. If the variable is global then you may use it everywhere (don't forget to call
free
when you no longer need the string). :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]No,I am not able to use the globally declared sz123[0] variable in another class: In class1: LPSTR sz123[10]; char c[20] = "test"; sz123[0] = (LPSTR)malloc(sizeof(c[0]) * (strlen(c) + 1)); strcpy(sz123[0], c); In class2: extern LPSTR sz123[10]; char ch1[20]; strcpy(ch1,sz123[0]); It is showing the following error "error LNK2001: unresolved external symbol "char * * sz123":
-
Karthika85 wrote:
Is it possible to use the globally declared LPSTR variable in other class by using extern function?
You're mixing a bit different domains here. If the variable is global then you may use it everywhere (don't forget to call
free
when you no longer need the string). :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
No,I am not able to use the globally declared sz123[0] variable in another class: In class1: LPSTR sz123[10]; char c[20] = "test"; sz123[0] = (LPSTR)malloc(sizeof(c[0]) * (strlen(c) + 1)); strcpy(sz123[0], c); In class2: extern LPSTR sz123[10]; char ch1[20]; strcpy(ch1,sz123[0]); It is showing the following error "error LNK2001: unresolved external symbol "char * * sz123":
You must be precise. A global variable cannot be declared inside a class (it wouldn't be global). So what do you intend to do? Do you want to use a global variable? Do you want to make an object's member variable available to objects of different classes? :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Of course. The
static
qualifier, for varibles declared outside classes, makes the variables themselves having file-scope. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
You must be precise. A global variable cannot be declared inside a class (it wouldn't be global). So what do you intend to do? Do you want to use a global variable? Do you want to make an object's member variable available to objects of different classes? :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
I decalred a global variable array not class variable in one class and i want to use that gloabl variable in another class.
And what's your problem? For instance:
// global.cpp
int global_counter=0;and
// source.cpp
extern int global_counter;void show_counter()
{
cout << global_counter << endl;
}If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
And what's your problem? For instance:
// global.cpp
int global_counter=0;and
// source.cpp
extern int global_counter;void show_counter()
{
cout << global_counter << endl;
}If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
No,I am not able to use the globally declared sz123[0] variable in another class: In class1: LPSTR sz123[10]; char c[20] = "test"; sz123[0] = (LPSTR)malloc(sizeof(c[0]) * (strlen(c) + 1)); strcpy(sz123[0], c); In class2: extern LPSTR sz123[10]; char ch1[20]; strcpy(ch1,sz123[0]); It is showing the following error "error LNK2001: unresolved external symbol "char * * sz123":