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,
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":