strcpy() issue

This is a discussion on strcpy() issue within the C Programming forums, part of the General Programming Boards category; I'm curious if this can work somehow? My goal would be having a dynamic string structure and have functions that ...

  1. #1
    Registered User philvaira's Avatar
    Join Date
    Apr 2004
    Posts
    72

    strcpy() issue

    I'm curious if this can work somehow? My goal would be having a dynamic string structure and have functions that operate it more extensively than the standard library. However, I don't want to delete a string after going out of scope every time. Is there any way for strcpy() to copy a value into a String.text? I'm getting an access violation exception right now when it enters strcpy().

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    typedef struct tagString
    {
    	char *text;
    } String;
    
    void String_SetText(String *ob, const char *value)
    {
    	strcpy(ob->text, value);
    }
    
    char *String_GetText(String *ob)
    {
    	return ob->text; 
    }
    
    int main(void)
    { 
    	String str; 
    
    	String_SetText(&str, "Hello World!");
    
    	printf("%s\n", String_GetText(&str));
    
    	system("pause");
    	return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You have to have memory before you can write to it. Your text member would point to memory, if you had any, but you don't. You need to first acquire memory, using malloc and friends, and then work with that memory.

  3. #3
    Registered User philvaira's Avatar
    Join Date
    Apr 2004
    Posts
    72
    I was afraid of that. Unfortunately that means having something like String_Release(&str); at the end of every function that initializes a new string. Is there any way around it?

    I could do this but it's static in memory.

    Code:
    typedef struct tagString
    {
    	char text[80];
    } String;
    Last edited by philvaira; 03-21-2009 at 02:03 PM.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by philvaira View Post
    I was afraid of that. Unfortunately that means having something like String_Release(&str); at the end of every function that initializes a new string. Is there any way around it?
    I don't see why it would mean anything of the sort. You wouldn't want to release the string until you're done with it.

    (Unless you mean that you are done with it, in which case you would want to indicate that by calling a function like String_Release.)

  5. #5
    Registered User philvaira's Avatar
    Join Date
    Apr 2004
    Posts
    72
    Yes, the second assumption is what I meant.

    I'm debating whether to do this or just keep a static array. I think malloc/free is the best way to go so I'm not wasting memory and I can resize if needed. Thanks for the help. I think it's ugly in some regard, for example:

    Luckily it doesn't get any worse

    Code:
    	String str; 
    
    	String_SetText(&str, "Hello World!");
    
    	printf("%s\n", String_GetText(&str));
    
    	String_Release(&str);
    Last edited by philvaira; 03-21-2009 at 02:19 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A Full Program to analyze.
    By sergioms in forum C Programming
    Replies: 2
    Last Post: 12-30-2008, 08:42 AM
  2. float calculation issue
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-26-2008, 04:56 AM
  3. type safe issue
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 02-12-2008, 08:32 PM
  4. Strcpy
    By Godders_2k in forum C Programming
    Replies: 17
    Last Post: 12-12-2007, 11:34 AM
  5. my first issue of GDM
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 09-12-2002, 04:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21