How to initilasize a string variable?

This is a discussion on How to initilasize a string variable? within the C Programming forums, part of the General Programming Boards category; Can anyone tell me? Thanks & Bst Rgds...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    8

    Unhappy How to initilasize a string variable?

    Can anyone tell me?

    Thanks & Bst Rgds

  2. #2
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    What do u mean?

    Do u mean:

    string[] = "hello";

    or

    string[ 6 ] = "hello";

    or

    *sPtr = "hello";

    ?

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    8

    Smile

    Sorry, I think I don't list my opinion clearly.

    Here is my program's situation, I need use the string variable repeatly in a loop like this:

    declare block :
    char linebuf[20];
    char *c;
    .
    .
    program block :
    c = linebuf;

    while(..)
    {
    while(...)
    {
    c = &linebuf[0];
    .
    *c = *p;/*p: another pointer to an input string*/
    p++;
    c++;
    .
    .
    }
    strcpy(another string, linebuf);
    }

    fputs(another string,outfile);


    if the infile's format like : 'aaaaaaaaaaaaaa'
    'bbbbbb '
    'c '
    .

    I found the result of the outfile is not which my needed. It'd like
    this format :
    'aaaaaaaaaaaaaa'
    'bbbbbbaaaaaaaa'
    'cbbbbbaaaaaaaa'
    .

    So I want to put the linebuf to null or space after each loop, what method can I use?

    Best Rgds.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,823
    The empty string is obtained by doing this

    buff[0] = '\0';

    Which is fine if you just use the likes of strcpy and strcat

    If you do your own char manipulations in the buffer, you might want to empty the whole buffer, like so

    memset ( buff, 0, sizeof(buff) );

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    8

    Thumbs up

    Hi,Salem

    Thank you very much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. assigning a string to a variable
    By xp5 in forum C Programming
    Replies: 12
    Last Post: 08-30-2007, 02:13 AM
  2. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 07:52 PM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 01:56 PM
  4. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 12:47 PM
  5. Variable Allocation in a simple operating system
    By awkeller in forum C Programming
    Replies: 1
    Last Post: 12-08-2001, 02:26 PM

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