Thread: make a char* which will hold newline

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    31

    make a char* which will hold newline

    I want to copy '\n' (i.e a newline) into a
    char *src
    (i.e to a pointer to a character). The apparent use will be to chcek this string with other strings I have for newlines. I need to keep it char* since my other strings are defined such.

    Code:
    char *newLineTest;
    strcpy(newLineTest, '\n');
    gives me a seg error saying that SIGSEGV,

    any idea?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You need to do
    src = malloc( some_number_of_bytes );

    Plus strcpy takes strings, not characters
    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.

  3. #3
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    You have not allocated any space to store the newline character. If you must use char* then you need to use malloc. Otherwise, do something like:

    Code:
    char src[size_you need];

  4. #4
    Registered User
    Join Date
    Oct 2004
    Posts
    31
    hi
    I used something llike this.. gives me SIGSEV, out of bound error
    Code:
        char *newlinetest=malloc(sizeof(4));
        newlinetest='\n';
    Apprently the strings that it will be comparing with can be as big as 100 chars. hence I also tried
    Code:
        char *newlinetest=malloc(sizeof(100*sizeof(char)));
        strcpy(newlinetest,'\n');
    both gave me same error

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    >strcpy(newlinetest,'\n');
    Read!
    '\n' isn't a string

    Code:
    strcpy(newlinetest,"\n");
    Use double quotes
    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.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    #include <stdio.h>
    
    int main()
    {
       printf("sizeof(100*sizeof(char)) = %d\n", (int)sizeof(100*sizeof(char)));
       return 0;
    }
    
    /* my output
    sizeof(100*sizeof(char)) = 4
    */
    [edit]If you mean 100, say 100.
    Code:
    char *newlinetest = malloc(100);
    Last edited by Dave_Sinkula; 10-10-2004 at 11:02 AM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Oct 2004
    Posts
    31
    yeep fixed thanks all again. you guys are wonderful
    Code:
     char *newlinetest=malloc(100*sizeof(char));
     strcpy(newlinetest,'\n');

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Still looks like single quotes to me
    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.

  9. #9
    Registered User
    Join Date
    Oct 2004
    Posts
    31
    oh yeah... it should be double quotes ""

    you know I am so tired.... that I am making more mistakes with time now.. damn

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf , char, newline, space
    By Harry Reve in forum C Programming
    Replies: 1
    Last Post: 09-11-2011, 06:49 AM
  2. Remove newline char at the end
    By Ducky in forum C++ Programming
    Replies: 9
    Last Post: 12-23-2009, 02:29 PM
  3. Replies: 19
    Last Post: 12-17-2007, 02:57 AM
  4. char strings make me mad
    By bennyandthejets in forum C Programming
    Replies: 7
    Last Post: 07-28-2002, 01:36 AM
  5. fgets and a bothersome newline char
    By ivandn in forum Linux Programming
    Replies: 1
    Last Post: 11-14-2001, 01:41 PM