|
kakan wrote: n debug, the allocated buffer becomes filled with null-bytes.
In release-build, it isn't. It's filled with junk characters (whatever is on the stack).
and this is the source of very weird bugs.
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.
|
|
|
|
|
Hello,
when i include a header file of a class in a ".c" file ...i get error ... "class name" is undefined identifier.
how do i solve this?
amy clues?
regards
Shailesh
|
|
|
|
|
class is C++ keyword. Why you want to use it in C ?
|
|
|
|
|
You're speaking Dutch to a Frenchman. In other words, no .c file can contain a class. There are no classes in C.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
Shailesh Halankar wrote: how do i solve this?
You cannot.
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.
|
|
|
|
|
Shailesh Halankar wrote: i include a header file of a class in a ".c" file
is that what u said, or are we getting u wrong?? you are trying to use classes in C??
May be u are not explaining correctly i guess.
vijay.
|
|
|
|
|
Shailesh Halankar wrote: when i include a header file of a class in a ".c" file
You make your file a .cc, .cpp, or .cxx file
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
Where am I going wrong? I am trying to implement strcpy manually.
<code>
main()
{
char *source = "Hi", *dest = "";
while(*source++) *dest++ = *source;
}
Visual C++ 6.0 Compiler with service pack 6. WindowsXP 32 bit edition.
|
|
|
|
|
Aljechin wrote: *dest = "";
u havent allocated memory for the dest
nave
|
|
|
|
|
Neither have I allocated memory for the source...
char *source = "Hi", *dest = "";
dest = "Hello world";
This is working fine... Someone please tell me why is it crashing if i enable that while loop.
|
|
|
|
|
Aljechin wrote: Neither have I allocated memory for the source...
Yes you have. source is 3 characters long, "Hi" and the terminating null byte.
modified:
Or rather, source is pointing to a memory area which is initialized to "Hi" and the terminating null byte.
Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson
|
|
|
|
|
kakan wrote: Yes you have. source is 3 characters long, "Hi" and the terminating null byte.
That way even the dest has the terminating null byte. ( "" == '\0' ).
Sorry but I am just curious why it's not working? I am not expert like you guys. Please help me.
I tried declaring *dest as "Halo" and it still crashes.
|
|
|
|
|
You shouldn't do it this way. Try this instead:
main(){
char temp[30];
char *source = "Hi", *dest = temp;
while(*source++) *dest++ = *source;
}
Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson
|
|
|
|
|
kakan wrote: You shouldn't do it this way. Try this instead:
Please tell me where exactly was I going wrong. Can you show me a sample to implement strcpy this way? I am trying like mad since half an hour.
|
|
|
|
|
This really is basics, I suggest you read a book that covers C. It would make things clearer to you.
Your original code was this:
main()
{
char *source = "Hi", *dest = "";
while(*source++) *dest++ = *source;
}
A declaration like this:
char *source = "Hi"
is (normally) used for constant values (that doesn't change during the execution of the program).
If you want a buffer that can be modified, you'd better allocate it, like this:
char temp[50];
Now you have a buffer that can hold up to 50 characters. Use it as you wish, i.e. like this (as long as your source text doesn't exceed 49 charaters plus the terminating null character):
strcpy(temp, "Hello World");
Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson
|
|
|
|
|
HI
me also trying for it...
its working release version not in debug version why is that?????
|
|
|
|
|
The original code stomps memory that it doesn't own. In release mode, the behaviour is undefined, in debug, the IDE is watching if you do this, and letting you know that you've done it.
The release mode version will crash, sometimes.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
HI
So why is char * source = "Hello" giving problem
why am i able to just read memory not write to it????
|
|
|
|
|
You can write to it, I believe. The problem is, it's a fixed memory location, of fixed size. You can't reassign the string to a longer length. It's bad practice to define a string this way, unless you expect it to be a constant in your program.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
Aljechin wrote: Sorry but I am just curious why it's not working? I am not expert like you guys. Please help me.
What are you not understanding? It's been explained several different ways. In your code snippet, dest is not pointing to a valid (i.e., allocated) memory location. Until it is, you cannot copy characters to it from source . strcpy() would behave in the same fashion had you used:
char *source = "Hello", *dest = "";
strcpy(dest, source); One solution is to use:
char *source = "Hello", dest[6] = "";
strcpy(dest, source); Another would be:
char *source = "Hello", *dest = new char[6];
strcpy(dest, source);
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
There are two problems.
1. Your char* variables are initialized to point at string literals. You should never write to such memory, as the compiler may store the strings in read-only memory, causing a crash if you write to them.
2. Even if the memory were writable, dest only has room for one char. When you copy source into it, you're writing off the end of the array.
|
|
|
|
|
Thank you very much, I understand it now. I did not give you that 2 vote. I am checking the answer today only.
|
|
|
|
|
Hi!!!
I am just beginner in this field. I want to search for one specified file from the folder. as in I have one file called “WG_LL.txt “in my file.. n in each Webdoku section this file is contain inside loopo folder..ie File WG_LL.txt file is always inside of loopo folder but the location of loopo folder is different.
For example…
I created one dialogue box… and it contain my Webdoku section and path of the section..
Webdoku section Path of the section
DEMO70E D:\Progsample\ProdV70E\ProdMast\
DEMO70D C:\project\webdoku\
SAMPLE D:\Prog. sample\ProdV70E\WebDoku\
DEMO80D E:\blabla\webdoku\
DEMO60E D:\Progsample\WebDoku\
WT1 \\rekafs\Projekte_USB\myfolder\9_WebDoku\
WT2 \\rekafs\Projekte_USB\myfolder\9_WebDoku\
If suppose, I select DEMO70E from my dialogue box then it should be search for “WG_LL.txt file” and here path of the file is …D:\Progsample\ProdV70E\WebDoku\loopo\WG.LL.txt.
If suppose I select DEMO60E, then it should be search for file “WG_LL.txt” and path of the file is D:\Progsample\WebDoku\loopo\WG_LL.txt..
If suppose I select WT1, then it should be search for file “WG_LL.txt” and path of the file is \\rekafs\Projekte_USB\myfolder\9_WebDoku\WT1\loopo\WG_LL.txt
In short, here I want to search for loopo folder coz WG_LL.txt” file will be contain in Loopo folder…Here I have some code in this file (WG_LL.txt) so I need this file.
In my method…my section = selected Webdoku section (for example..DEMO70E)
&
Mypath =selected section path ( D:\Progsample\ProdV70E\ProdMast\)
So how can I find that file with location of the path.
Thank you very much…
Shah Satish
|
|
|
|
|
If you want to search for a file you can use of
FindFirstFile/FindNext<code> <br />
<br />
<div class="ForumSig"><div style="padding: 0; margin: 0; background-color: rgba(170, 214, 255, 1); text-align: center; font-family: Arial; font-weight: bold; font-style: italic; font-size: 9pt"><br />
<div style="color: rgba(255, 255, 255, 1); width: 300px"><br />
<hr width="250" size="7">WhiteSky<hr width="220" size="7"></div></div></div>
|
|
|
|
|
Hi!!
thanks fro your reply..
In short, I want to search file called "WG_LL.txt" from specified path..
see my code...
if i select DEMO70E then specified path is
D:\progrsample\prodv70E\prodmast...
so i want to search that file in this directory..(D:\progrsample\prodv70E\prodmast...)
Thank you..
Shah Satish
|
|
|
|