Thread: a problem with array of char

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    30

    a problem with array of char

    Hi, all,

    I have the following problem:
    Code:
    int read_line(char *buf) {
        int max_size = findmaxsize();
        char result[max_size];
    
        // put some char into result
    
        buf = result;
    }
    It seems that buf and result contain nothing. I must do it like the following to get correct output.

    Code:
    int read_line(char *buf) {
        //int max_size = findmaxsize();
        char result[1024];
    
        // put some char into result
    
        buf = result;
    }
    However i need the max_size, cos everytime this value changes. Can anyone help me to solve this problem? Thank you so much!

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Code:
       int max_size = findmaxsize();
        char result[max_size];
    You can't do that in C. You're attempting to create a C-string of a certain length at runtime. In order to do that, you need to allocate memory for it. You can use malloc() to do that.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You've got multiple issues here, aside from what was already mentioned.

    1) You're assigning a passed pointer a different value. This has absolutely no effect outside of the function. To change a value inside a function, and have it keep outside the function, you have to pass a pointer to that object. Therefore, if you want to change what a pointer outside a function points to inside a function call, you have to pass a pointer to a pointer.

    2) You're assigning what would be a local variable to a pointer. The local instance is actually destroyed as soon as the function ends, leaving your pointer (assuming it would in fact update the pointer--which it won't, as listed at point 1) pointing to a non-existant place in memory. That's bad.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Feb 2005
    Posts
    30
    But i also cannot code like
    Code:
    char *result[max_size];
    int i;
    for(i = 0; i < max_size; i++)
        result[i] = (char *)malloc(sizeof(char));
    cos i also need max_size at run-time. right?
    so how can i do?

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What exactly are you trying to do? Just get a user defined string length?
    Code:
    int x;
    char *mystr;
    
    x = getsizefromuserplusoneforthenull();
    
    mystr = malloc( x );
    
    ...do whatever it is with this string...
    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Feb 2005
    Posts
    30
    Quote Originally Posted by quzah
    You've got multiple issues here, aside from what was already mentioned.

    1) You're assigning a passed pointer a different value. This has absolutely no effect outside of the function. To change a value inside a function, and have it keep outside the function, you have to pass a pointer to that object. Therefore, if you want to change what a pointer outside a function points to inside a function call, you have to pass a pointer to a pointer.

    2) You're assigning what would be a local variable to a pointer. The local instance is actually destroyed as soon as the function ends, leaving your pointer (assuming it would in fact update the pointer--which it won't, as listed at point 1) pointing to a non-existant place in memory. That's bad.

    Quzah.
    Yes, you are correct! buf does not point to a valid value. But i cannot change the header int read_line(char *buf) cos it is a fixed API given.

    So how can i make buf points to an array of char whose lengh is max_size?

    Thank you so much!!

  7. #7
    Registered User
    Join Date
    Feb 2005
    Posts
    30
    Quote Originally Posted by quzah
    What exactly are you trying to do? Just get a user defined string length?
    Code:
    int x;
    char *mystr;
    
    x = getsizefromuserplusoneforthenull();
    
    mystr = malloc( x );
    
    ...do whatever it is with this string...
    Quzah.

    It works..

    Thanx Quzah!! You are amazing!!

  8. #8
    Registered User
    Join Date
    Feb 2005
    Posts
    30
    Another problem is, when i use
    Code:
    int read_line(char *buf) {
        int max_size = findmaxsize();
    
        buf = malloc(max_size + 1);
        // do something about buf...
    
        printf("%s\n", buf);
    }
    buf is printed out correctly here. However, when i printf buf outside the function, it does not print correctly :
    Code:
    char *buf;
    read_line(buf);
    printf("%s\n", buf);
    Is it that we malloc for buf inside the function, but outside the function the memory is freed, so buf is no longer point to the correct memory?

    How to solve this problem?

    And when i use malloc(0), or malloc(1).... they will be same as i use malloc(max_size + 1). Why is it like this?

    Thank you...

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    You need to keep track of the result that malloc gives you. Discarding the location of the memory you received is a bad thing to do.
    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.*

  10. #10
    Registered User
    Join Date
    Feb 2005
    Posts
    30
    How to keep track the result malloc gives me?

    buf points to the location of malloc inside the function read_line(buf), but how come it does not point to the same location outside the function? or it points to the same location, but i dont have the priviledge to printf it? how can i do?

    Thank you...

  11. #11
    Registered User
    Join Date
    Apr 2004
    Posts
    173
    Well if you could change the header to:

    Code:
    int read_line(char **buf) {
        int max_size = findmaxsize();
    
        *buf = malloc(max_size + 1);
        // do something about buf...
    
        printf("%s\n", *buf);
    }
    But since you said you can't change it then I'm not sure

  12. #12
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Or return buf.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    char *foo(char *buf)
    {
       static const char text[] = "hello world"; /* simulation of other stuff */
       buf = malloc(strlen(text) + 1);
       if ( buf )
       {
          strcpy(buf, text);
          printf("foo:  buf = \"%s\"\n", buf);
       }
       return buf;
    }
    
    int main(void)
    {
       char *ptr = foo(ptr);
       if ( ptr )
       {
          printf("main: ptr = \"%s\"\n", ptr);
          free(ptr);
       }
       return 0;
    }
    
    /* my output
    foo:  buf = "hello world"
    main: ptr = "hello world"
    */
    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.*

  13. #13
    Registered User
    Join Date
    Feb 2005
    Posts
    30
    Hi, Dave_Sinkula,

    Your method works. But the header is a fixed API, i cannot change int read_line(char *buf) to char *read_line(char *buf)...

    Is there another way to do it?

    Thank you...

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Pass an array to the function, and fill that inside the function. As stated, you cannot make a pointer outside a function point to something new inside a function if you don't pass a pointer to that pointer.

    Since you can't change your API, you have no other choice but to pass to the function the memory you need to fill. That is to say, you have to write another function to take the size of the needed input, create a string, and then pass that string to the read line function, modifying its contents.

    This however presents a problem for you, since inside the function, since you can't change your API, you will have no way of knowing how long the input is.

    In other words, whoever designed your API was an idiot.

    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by SoFarAway
    Is there another way to do it?
    Global variables? Otherwise...

    Nope. You're hosed. If you need to do anything with the string, it must be done in read_line. Throw the malloc problem onto the others quzah already listed.
    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.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Char array problem.
    By kbro3 in forum C++ Programming
    Replies: 16
    Last Post: 08-21-2008, 01:31 PM
  2. problem working with char array
    By bradleym83 in forum C Programming
    Replies: 4
    Last Post: 07-27-2005, 10:57 AM
  3. Problem Putting INTs Into a CHAR Array
    By cram in forum C++ Programming
    Replies: 13
    Last Post: 10-13-2004, 07:53 AM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM