|
strcpy copies the value of a string, I don't believe it actually allocates the memory for you. You need to make str2 point to valid memory first.
Christian Graus - C++ MVP
|
|
|
|
|
sorry i have modified my previous post. i do get a warning.
passing arg 1 of `strcpy' discards qualifiers from pointer target type
|
|
|
|
|
probably the const
Christian Graus - C++ MVP
|
|
|
|
|
hi...I tried doing the following:
const char* str1 = "abcd";
char* str2;
strcpy(str2,str1);
There is no compilation error or warning. but the program crahses at this stcpy line. Why would it?
Can I do this?
str2= str1;
will this work? what happens when i dothis?
|
|
|
|
|
*sigh* please read my answer from before. str2 should NEVER be declared like this, set all pointers to NULL until you give them a valid value. str2 is not valid, and it's pointing at junk, hence the error.
Christian Graus - C++ MVP
|
|
|
|
|
thanks. fixed it.
i need to split a string in to tokens using delimiter but without using the strtok( ) or the strtok_r functions.
ie., const char * str1 = "apple+mango+grapes+orange";
i need to spli them into
apple
mango
grapes
orange
the delimter here is "+".
But is there a way (code snippet) that would do the above without using the strtok functions?
|
|
|
|
|
Yes, you can do what strtok does, make a temporary pointer, and keep pointing to the place past the last delimited ( first replacing the delimiters with NULL ).
Christian Graus - C++ MVP
|
|
|
|
|
Thanks very much for your help. It has been a good learning experience.
Back to my sqaure1 problem,,,,In the following code, the names are null when i try to access them inside funcB. why?
Should I do malloc for Names? if so, how?
funcA{<br />
array = 4;<br />
char * Names[array];<br />
int next = 0;<br />
int i;<br />
<br />
for(i=0;i<array+1;i++)<br />
{<br />
Names[i]=NULL;<br />
}<br />
<br />
char * list = "Abc";<br />
Names[ next ] = malloc( sizeof( char )*( strlen( list ) + 1 ) );<br />
memcpy( Names[ next ],list,sizeof( char )*( strlen( list ) + 1 ) );<br />
<br />
list = "cdefg";<br />
Names[ next+1 ] = malloc( sizeof( char )*( strlen( list ) + 1 ) );<br />
memcpy( Names[ next+1 ],list,sizeof( char )*( strlen( list ) + 1 ) );<br />
<br />
printf("vsp list got is %s\n", Names[0] );<br />
printf("vsp list got is %s\n", Names[1] );<br />
<br />
funcB(....,...,(const char**)&Names);<br />
}<br />
<br />
funcB(...,...,const char ** names)<br />
{<br />
....<br />
...<br />
free( (char*)names[0]);<br />
free( (char*)names[1]);<br />
<br />
}
|
|
|
|
|
thathvamsi wrote: funcB(....,...,(const char**)&Names);
I've told you at least twice to do this:
funcB(....,...,(const char**)Names);
because your array IS a char **, you're passing a char ***.
Christian Graus - C++ MVP
|
|
|
|
|
I did and it still gives null only.
|
|
|
|
|
To be honest, I tried to do a sample project for you, and found that VC will not compile most of your code. What you've posted, does not compile, and cannot compile. Even fixing the typos, VC will not compile it, another ( very non conformant ) compiler might, but VC will not.
Try creating a char **, like I said in the first place.
Christian Graus - C++ MVP
|
|
|
|
|
This actually works just fine. Obviously, all funcB does is free the names, but they are correctly being passed through.
#include <string.h>
#include <malloc.h>
void funcB(const char ** names)
{
//the actual values names[0] and names[1] are to be accessed and used but they are null!!!???
//free names[0] and names[1]
free( (char*)names[0]);
free( (char*)names[1]);
}
void funcA()
{
int array = 4;
char ** Names = new char *[array];
int next = 0;
int i;
//initialise
for(i=0;i < array; ++ i)
{
Names[i]=NULL;
}
char * list = "Abc";
Names[ next ] = (char*) malloc( sizeof( char )*( strlen( list ) + 1 ) );
memcpy( Names[ next ],list,sizeof( char )*( strlen( list ) + 1 ) );
++next;
list = "cdefg";
Names[ next ] = (char*) malloc( sizeof( char )*( strlen( list ) + 1 ) );
memcpy( Names[ next ],list,sizeof( char )*( strlen( list ) + 1 ) );
printf("vsp list got is %s\n", Names[0] );
printf("vsp list got is %s\n", Names[1] );
funcB((const char**)Names);
}
int main(int argc, char* argv[])
{
printf("Hello World!\n");
funcA();
return 0;
}
Christian Graus - C++ MVP
|
|
|
|
|
Hi...
Thanks for your time and help. I fixed it and Iam using char** Names but obviously, there is a bug with teh gcc compiler for it allowed me to do char*Names[array];
and returned no error or warnings.
But the program works now.
Thanks again. You have been a great help to me in understanding pointers and arrays and also gcc.
|
|
|
|
|
No worries. If it accepted 'new', then it's compiling your code as C++.
I'm glad I helped. You'd do well to buy a book, or at least read up on the web on this stuff, to solidify the stuff we've worked on.
Christian Graus - C++ MVP
|
|
|
|
|
Bear in mind that I used 'new' and you'll need to use 'malloc'
Christian Graus - C++ MVP
|
|
|
|
|
Hi i used malloc instead of new as it is C.
Yup, Iam thinking of buying a book.
Can you recommend some good books worth buying and worth reading?
|
|
|
|
|
The Kernegan (sp?) Ritchie book is the classic book on C. I learned C AFTER C++, so I did it entirely by working out which bits were missing, I have no other recommendations on C books.
Christian Graus - C++ MVP
|
|
|
|
|
The Kernegan (sp?) Ritchie book is the classic book on C. I learned C AFTER C++, so I did it entirely by working out which bits were missing, I have no other recommendations on C books.
Christian Graus - C++ MVP
|
|
|
|
|
Thanks for that.
|
|
|
|
|
char * Names[] = {};
where is this statement is placed?
in global scope? or inside the mail?
or as mentioned in the question?
If the definition of Names is done in funcA, then the scope of Names&lifetime would be lost.
I'm very sure if the definition is at main(), or global then it will work, provided that funcA is called before funcB.
To solve that u can move
char * Names[] = {};
to global scope or to main() and pass this as argument to the funcA()
Please let me know if I'm wrong.
--
Reddy
|
|
|
|
|
iam working in multithreaded environments and hence
i have to declare the Names inside funcA only. and call func B within funcA.
|
|
|
|
|
I doubt if ur program compiles!
instead u can try with this!!
#define ARRAY_SIZE 10
char *Names[ARRAY_SIZE] = {0,};
because in ur stmt for defining the Names, no size has been mentioned and causes compilation errors.
or u can try like this
char **Names = (char**)malloc(sizeof(char*)*ARRAY_SIZE);
then it should work.
--
Reddy
|
|
|
|
|
Hi,
I'm pulling my hair out trying to do something simple. Thanks to the complicated and cryptic function signatures of MS with all that _in _out stuff I can't figure out how to do the following:
int pageNumber = 1;
int numPages = 2;
wstring wstr2Display; // I want to make this: L"1/2"
wstr2Display = ToString(pageNumber) + L"/" + ToString(numPages);
Seems I also can't directly 'add' the L"/".
Can someone share with me how to do this?
Thanks
|
|
|
|
|
erm... I'm not sure where Microsoft comes into this ? Is this mananged C++, or is ToString your own function ? What is ToString returning ?
Christian Graus - C++ MVP
|
|
|
|
|
MS only came into it because I can't read the cryptic help with all the extra stuff in the function signatures.
ToString() is just a 'dummy' function to allow me to write the line to show what I'm trying to do.
Actually, turns out what I really need is just to be able to concatenate and int + WCHAR + int into a WCHAR*.
Then I can use that WCHAR* to call GdiPlus::Drawstring()
So, in short, how do I convert an int to a WCHAR* representation?
|
|
|
|