Thread: assign value to string

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    58

    assign value to string

    Hey ,i was trying to assign a value to a string.

    For example lets say: int X; x=10;
    then when we do X=40, the value 40 overwrites the value 10;

    SO I was doing the same for string.
    my code was :
    Code:
    char *temp;
    
    temp=" ";
    
    temp[0]='a';     temp[1]='r';    temp[2]='t';     temp[3]='\0';
    printf("\n*%s--------->length is %d",temp,strlen(temp));
    
    temp=" ";
    temp[0]='i';     temp[1]='t';  temp[2]='\0';
    printf("\n*%s--------->length is %d",temp,strlen(temp));
    
    return 1;
    In unix it compiles without errors:but crashes when you run it.

    Was using gcc -ansi -pedantic -Wall -o runFile test.c


    though In Microsoft visual c++ 2003 it compiles and runs and in dev c it compiles but cant run.

    CAN SOME ONE HELP ME CORRECT MY MISTAKE ABOUT SO IT CAN RUN IN UNIX ?
    thanx alot

  2. #2
    Registered User
    Join Date
    Jan 2006
    Location
    Europe/Belgrade
    Posts
    78
    You have to reserve a memory for temp with malloc(), and free it at the end of the program. Without this, temp is a dangling pointer, and program crashes. Or, you can declare temp as an array.
    Last edited by karas; 04-06-2006 at 01:33 AM.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > temp=" ";
    It is implementation specific as to whether strings are writable
    If they are, then you can change them with temp[0] = 'a';
    If they are not, then you get a segfault.

    In any event, your other temp assignments beyond temp[1] are out of bounds.

    Stick to
    char temp[10];
    until you're more comfortable with pointers.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    wow that class on data structures must be work ing i actually understand how to do that without having to just use char temp[10]

    ya just use that for now easiest to start with
    hooch

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM