0

I am trying to create a string in C, which is just an array of data type char. I am trying to have a pointer and then assign the value of a char array. This is what I have so far:

char *string;
string = (char *)malloc(sizeof(char));

// Now I have a pointer, so if I wanted to print out the pointer of the spot 
// in memory that is saved I can do the following:
printf("%p", string);

// That gives me the pointer, now I want to assign an array at that address

// *string gives me that data stored at the pointer
*string = "Array of chars?";
printf("%s", *string);

I am wondering what I am doing wrong with that?

I need to use malloc unfortunately, but please feel free to tell me a better way to do this as well with along with the solution using malloc.

Thank you guys!

9
  • 1
    malloc returns a pointer of memory that is allocated on the heap. In your case, you just need to copy the characters into the buffer after allocation. Lookup strncpy, strcpy, or memcpy. Commented Apr 6, 2015 at 2:36
  • Since the first two lines won't even compile (not a good start), I think the first thing you're doing wrong is not studying pointers in the C language enough to begin effectively using them. Commented Apr 6, 2015 at 2:36
  • @WhozCraig I just threw out whatever was on the top of my head. I don't appreciate you not giving an answer and just shooting down my question. Yes to get it to properly compile I would need to remove the []. I'll fix my question so maybe you can give answering it a shot. Sorry about that. Commented Apr 6, 2015 at 2:39
  • 1
    There you go. So . How much data does the string you're going to copy occupy, and how much did you actually allocate? After fixing your decls, you're not too far off. You need to allocate the appropriate amount of memory (and lose the (char *) cast; it isn't needed in C), then do what Paul suggested; lookup the strcpy family of functions. Also, printf using %s requires a const char* compatible argument; you're giving it a char. Maybe fix that too. Commented Apr 6, 2015 at 2:41
  • 1
    The size of the string buffer allocation should be determined by the length of the string you're trying to copy +1 for the closing terminator. strlen will come in handy there (or just strdup() if allowed by your prof). If you're doing this in a linked list you're going to end up with two allocations per node (one for the node; one for the string field in the node). There are many examples of linked list node management on the web, not related to your question. Commented Apr 6, 2015 at 2:46

1 Answer 1

3

Instead of the two variables you declared, you should write:

char* string = malloc(sizeof(char) * <insert number of chars plus one here>);

You should write:

string = "Array of chars";
printf("%s", string); 

to print the string.

Sign up to request clarification or add additional context in comments.

5 Comments

Does it matter where the * is? You have it right next to char?
@Miguel J. No, I just prefer to have it that way.
So right now with your implementation the code looks like this: string[ ] ----> [Array of chars] right?
If I want to change the pointer I would use *string = new pointer location in memory and to change data at pointer would I use string = "new array of chars"?
@MiguelJ. Well, you should use free(string) first to clear the memory at the current location. Then you can assign it to another memory location like this: string = <insert name of pointer here>;

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.