Thread: issue pointer in c

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    6

    issue pointer in c

    Code:
    #include <stdio.h>
    
    #define MAXLINE 1000
    
    void str_cat(char *s, char *t);
    int getchars(char *s, int max);
    
    int main(void)
    {
        char *s, *t;
    
        printf("input string s: \n");
        while (getchars(s, MAXLINE) == 0)
            ;
        printf("input string t: \n");
        while (getchars(t, MAXLINE) == 0)
            ;
    
        str_cat(s, t);
        printf("cat: %s\n", s);
    
        return 0;
    }
    
    void str_cat(char *s, char *t)
    {
        while (*s)
            ++s;
        while (*s++ = *t++)
            ;
    }
    
    int getchars(char *s, int max)
    {
        int c, i, l;
    
        for (i = 0, l = 0; (c = getchar()) != EOF && c != '\n'; ++i)
            if (i < max - 1) {
                *s++ = c;
                ++l;
            }
        *s = '\0';
    
        return l;
    }
    when i type "first input string s" have
    the issue "test.exe has stopped working"
    can some one else help me solve this problem
    thanks

  2. #2
    Registered User
    Join Date
    Jun 2010
    Posts
    24
    You haven't initialized s to point to a specific locaion in memory where your input will be written to. Same also goes for t. You them to point to buffers where your input will be written to.
    Last edited by Golf7; 10-11-2013 at 07:41 AM.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    if you are telling your getcahr function that buffer can store MAXLINE characters - you need a buffer that can store at least this many characters

    Code:
        char s[MAXLINE];
        while (getchars(s, MAXLINE) == 0)
    or it could be
    Code:
        char s[MAXLINE];
        while (getchars(s, sizeof(s)) == 0)
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Sep 2013
    Posts
    6
    s have initialized
    but t is same thing happen
    if i initialized t also
    i start run it, also have same issue come out

  5. #5
    Registered User
    Join Date
    Sep 2013
    Posts
    6
    Code:
    char *s, *t;
    i change to
    Code:
    char s[MAXLINE];
    char t[MAXLINE];
    after i change i can run the program
    thanks to help ^_^

  6. #6
    Registered User
    Join Date
    Jun 2010
    Posts
    24
    You need both s and t pointing to buffers where your input is stored. One way of doing this is to use arrays as suggested above by vart which I see you have now done.
    Last edited by Golf7; 10-11-2013 at 07:42 AM.

  7. #7
    Registered User
    Join Date
    Sep 2013
    Posts
    6
    Golf7 "You need both s and t pointing to buffers where your input is stored."
    may i know you say that is how do to pointing to buffers ?
    can you guide me
    thanks

  8. #8
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Pointers point to addresses in memory so you need an adressed block of memory to point to.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer to Pointer manipulation issue
    By workisnotfun in forum C Programming
    Replies: 5
    Last Post: 10-16-2012, 11:31 AM
  2. Pointer Issue?
    By asdfgtrew in forum C++ Programming
    Replies: 2
    Last Post: 04-04-2012, 08:13 AM
  3. Pointer issue
    By pfs in forum C Programming
    Replies: 2
    Last Post: 09-26-2008, 11:37 AM
  4. Pointer issue when looking through .txt
    By kordric in forum C++ Programming
    Replies: 1
    Last Post: 05-17-2008, 11:26 AM
  5. C++ pointer issue
    By yongzai in forum C++ Programming
    Replies: 8
    Last Post: 12-22-2006, 04:27 AM