Thread: whats the deal here?

  1. #1
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211

    whats the deal here?

    hey all, figured I'd post this in the C board because I'd probably get a better result, since it seems to be a problem caused by a C standard library function declared in string.h.

    first let me show you some declarations:

    name is declared as a member of a pfile struct, which is a pointer to a struct that is declared globally. name is declared as:

    Code:
    char name[_MAX_FNAME];
    p is declared as a local variable inside of the function that I am attempting this in as:

    Code:
    char *p = "";
    now here is what I am attempting:

    Code:
    p = PathFindFileName(szListData[0][0]);
    	strcpy(pfile->name, p);
    
    	MessageBox(NULL, pfile->name, NULL, MB_OK);
    yes, there are Win32 APIs in there, but for now, just pretend they are functions written by myself to perform the actions made obvious by their names. they are not the problem. the problem is the strcpy statement.

    when I compile this code, it compiles 0 errors 0 warnings, yet when I execute it, I get an error from Windows saying that the application needs to be closed.

    yet when I compile and execute this code:

    Code:
    p = PathFindFileName(szListData[0][0]);
    
    	MessageBox(NULL, p, NULL, MB_OK);
    it prints the filename successfully without any problems (hence how I know it is not the APIs causing the problem).

    the problem seems to lie in the strcpy statement, which I just can't understand. that code looks 100% legal to me, along with everybody else I have showed it to.

    could anyone please tell me what the heck could be happening here?

    any help at all would be greatly appreciated. thank you in advance.

    Intel Core 2 Quad Q6600 @ 2.40 GHz
    3072 MB PC2-5300 DDR2
    2 x 320 GB SATA (640 GB)
    NVIDIA GeForce 8400GS 256 MB PCI-E

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    How is the memory pointed to by pfile allocated?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211
    LOL. it isn't. pfile points to nothing .

    thanks Dave, my pointer n00bness strikes again.

    I should just stop using them until I am really used to using them. its all good learning experience though.

    Intel Core 2 Quad Q6600 @ 2.40 GHz
    3072 MB PC2-5300 DDR2
    2 x 320 GB SATA (640 GB)
    NVIDIA GeForce 8400GS 256 MB PCI-E

  4. #4
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Another thing is (I think) that PathFindFileName returns a LPTSTR, which I think isn't the same thing as a char *. . . If I'm not mistaken (I could be, it has been a long day), you'll have to convert that LPTSTR to a regular char * first. strcpy() will copy bytes, not LPTSTR.

  5. #5
    Registered User
    Join Date
    Jun 2004
    Posts
    201
    Quote Originally Posted by Kennedy
    Another thing is (I think) that PathFindFileName returns a LPTSTR, which I think isn't the same thing as a char *. . . If I'm not mistaken (I could be, it has been a long day), you'll have to convert that LPTSTR to a regular char * first. strcpy() will copy bytes, not LPTSTR.
    LPTSTR is a macro that expands to char * or wchar_t * depending on if _UNICODE is defined.
    In the latest VisualC++ the default is that _UNICODE is defined so this code wouldnt compile anymore

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Also, this is bad practice:
    Code:
    char *p = "";
    Although C allows it, you should never assign a string literal to a non-const pointer.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Deal or No Deal listbox prob
    By kryptkat in forum Windows Programming
    Replies: 5
    Last Post: 03-30-2009, 06:53 PM
  2. pls fix my deal or no deal program
    By llinocoe in forum C Programming
    Replies: 5
    Last Post: 09-23-2008, 11:37 AM
  3. pls healp deal or no deal
    By llinocoe in forum C Programming
    Replies: 2
    Last Post: 09-19-2008, 12:23 PM
  4. Replies: 5
    Last Post: 09-18-2008, 02:57 PM
  5. whats the deal with EOF really ???
    By gemini_shooter in forum C Programming
    Replies: 7
    Last Post: 03-06-2005, 04:04 PM