make a char* which will hold '\n' ...

This is a discussion on make a char* which will hold '\n' ... within the C Programming forums, part of the General Programming Boards category; I want to copy '\n' (i.e a newline) into a char *src (i.e to a pointer to a character). The ...

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

    make a char* which will hold '\n' ...

    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,499
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,499
    >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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,499
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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. How to make a Packet sniffer/filter?
    By shown in forum C++ Programming
    Replies: 2
    Last Post: 02-22-2009, 08:51 PM
  2. HELP!wanting to make full screen game windowed
    By rented in forum Game Programming
    Replies: 3
    Last Post: 06-11-2004, 04:19 AM
  3. make all rule
    By duffy in forum C Programming
    Replies: 9
    Last Post: 09-11-2003, 01:05 PM
  4. Question about atheists
    By gcn_zelda in forum A Brief History of Cprogramming.com
    Replies: 160
    Last Post: 08-11-2003, 11:50 AM
  5. Replies: 6
    Last Post: 04-20-2002, 06:35 PM

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