char *, accessing/assigning problem

This is a discussion on char *, accessing/assigning problem within the C++ Programming forums, part of the General Programming Boards category; Code: typedef char * Str; int main() { Str name = "joe name[1] = 'i'; cout << name << endl; ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    16

    char *, accessing/assigning problem

    Code:
    typedef char * Str;
    int main()
    {
    Str name = "joe
    
        name[1] = 'i';
        cout << name << endl;
    return 0;
    }

    Can someone explain to me why it doesn't output "jie"? It gives me a memory error.

  2. #2
    FOX
    Join Date
    May 2005
    Posts
    188
    Because the string "joe" is stored as read-only in the code segment. All literal strings are stored read-only.

    There's a huge difference between:
    Code:
    char *string = "joe";
    and
    Code:
    char string[] = "joe";
    It's in the comp.lang.c FAQ:
    http://www.eskimo.com/~scs/C-faq/q1.32.html
    http://www.eskimo.com/~scs/C-faq/q16.6.html

  3. #3
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Well..

    1) Str is a pointer to a char, which you havent pointed to any memory or given its own memory to use.

    2) Str name = "joe, you forgot to close the quotation and use the semi colon.

    3) After using Str name = new char[10] (length of 10 chars for example), you will need to use strcpy (I believe) from .. <cstdlib> or one of those C #include files, that have functions to copy text to arrays.

    You can still edit them 1 char at a time though without using that #include:

    Code:
    #include <iostream>
    
    using namespace std;
    
    typedef char* Str;
    int main()
    {
        Str name = new char[10];
    
        name[1] = 'i';
        cout << name << endl;
        
        cin.get();
    return 0;
    }
    Not to suggest you should do this.. not really the most efficient thing to be doing, but if you wanted to do it the way you have in your topic post.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    16
    How would I make it Str a new type?

    typedef char[] Str;

    Str name;

    That doesn't work?

  5. #5
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Str is a pointer to a char, which you havent pointed to any memory or given its own memory to use.

    that's a valid assignment. (with the exception of the missing quote and semi)

    You can still edit them 1 char at a time though without using that #include:

    actually, this is how you would do it:
    Code:
    #include <iostream>
    
    typedef char* Str;      //this is probably just a waste of your time.
    
    int main()
    {
            Str name = new char[10];
    
            name[0]='i';    //if you assign one thing, it'll fill in the rest with '\0'
            name[1]='\0';   //you want to assign a null term, or you'll regret it
            std::cout << name << std::endl;
    
            delete[]name;   //DO NOT FORGET to delete memory you allocate
            return 0;
    }
    Last edited by major_small; 07-15-2005 at 04:34 AM. Reason: taipo
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conversion Char To Char * Problem
    By ltanusaputra in forum Windows Programming
    Replies: 3
    Last Post: 03-01-2008, 02:06 PM
  2. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM
  3. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 06:59 AM
  4. char problem
    By ForlornOdium in forum C++ Programming
    Replies: 10
    Last Post: 10-29-2003, 02:39 PM
  5. String Processing Problem
    By muffin in forum C Programming
    Replies: 0
    Last Post: 08-24-2001, 11:13 AM

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