Pointers and Strings

This is a discussion on Pointers and Strings within the C Programming forums, part of the General Programming Boards category; I've been struggling quite a bit to get some theory and code working together. You see, I was working on ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    157

    Pointers and Strings

    I've been struggling quite a bit to get some theory and code working together. You see, I was working on some code to use a char* as a string, and I get this error from GCC that I'm trying to go from char to integer or something without a case. Anyways, I'm not sure what I'm doing wrong or what I need to do to fix this code. Here is the faulty code:

    Code:
    char *test_ptr;
    char string[] = "This is a string";
    
    test_ptr = string;
    *test_ptr = "Th";
    What's wrong with this code? Why isn't it working? What do I need to do to get it working? Thanks!

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    You are trying to do an illegal assignment. What you have is a bunch of char's. What you are trying to assign to one of those char's is char[3]. Take a look at what you have.

    *test_ptr = "Th";

    What happens if you ouput *test_ptr? You will get 'T'. Because that is the single character stored there. Now you are trying to replace the character 'T' with 'T', 'h', '\0'. Remember when you use " " you automatically get the null terminator. You use single quotes to specify characters. So try changing that to...

    *test_ptr = 'T';

    If you still want the h, you'll need to increment the pointer and assign 'h' to the next value. Good luck.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    157
    okay, I wanted to put the whole string into the pointer at one shot. so, I changed this code to:
    Code:
    test_ptr = "Th";
    this is perfectly legal and correct, right? now test_ptr points to that string, correct? thanks.

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    157
    okay, i was just a little unsure and now i'm having some memory problems because windows shuts down an "illegal operation" and i think i'm just impeding somewhere in memory i shouldn't. i'll try to work that out.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with calloc and pointers to strings.
    By sweetarg in forum C Programming
    Replies: 1
    Last Post: 10-24-2005, 03:28 PM
  2. Arrays, pointers and strings
    By Apropos in forum C++ Programming
    Replies: 12
    Last Post: 03-21-2005, 11:25 PM
  3. Concatenating strings (dynamic array using pointers)
    By Tankndozer in forum C Programming
    Replies: 8
    Last Post: 07-01-2004, 08:27 AM
  4. hangman need help with strings and pointers
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 03-27-2002, 09:13 AM
  5. need more help with pointers and strings
    By bgbfflochp in forum C++ Programming
    Replies: 11
    Last Post: 03-19-2002, 08:31 AM

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