Thread: assign a string to char pointer

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    21

    assign a string to char pointer

    how would i assign a char pointer the value returned by a function that returns a string?

    Code:
    #include <stdio.h>
    
    const char *str(void);
    
    
    int main()
    {
    	char str2[50] *str1;
    
    	printf("%s\n",str());
    	
    	sprintf(str2, "%s",str());
    	printf("%s\n",str2);
    
    	sprintf(str1, "%s",str());
    	printf("%s\n",str1);
    
    	return 0;
    } 
    
    const char *str(void)
    {
    	return "this is a string";
    }

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    do you have a question?

    your last sprintf is wrong - it uses uninitialized pointer as destination buffer

    should be
    Code:
    const char* str2;
    
    ...
    
    
    str2 = str();
    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

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Assuming your function returns a pointer to valid memory, you would use "=" to assign it to something.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Except the function returns a const char*, so you should assign it to a const char*, and not a char*.
    const char* is NOT a string, nor is char*. That is a lesson you must learn. They are pointers to char. Typically, in C, also a pointer to a number of chars. The difference is that one is const, the other is not.
    And you should use a pointer to const, since you are returning a string literal, and they cannot be modified.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Dec 2009
    Posts
    1

    char pointer question

    I know that we can use char pointer so we don't have to have static number of characters as in char name[25] here we can't enter more than 24 chars.
    so, correct me if I'm wrong:
    char * name;
    cin>>name;//or cin.getline(name,25,'\n');
    cout<<name;

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    This is incorrect. A char pointer is not dynamic. You cannot work around the fact that you must have a static number of bytes in a buffer.
    SourceForge.net: Common mistakes and errors - cpwiki
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by fresh_girl View Post
    I know that we can use char pointer so we don't have to have static number of characters as in char name[25] here we can't enter more than 24 chars.
    so, correct me if I'm wrong:
    char * name;
    cin>>name;//or cin.getline(name,25,'\n');
    cout<<name;
    That's C++. This is the C forum.
    If you understand what you're doing, you're not learning anything.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Elysia View Post
    This is incorrect. A char pointer is not dynamic.
    Sure it is.
    Quote Originally Posted by Elysia View Post
    You cannot work around the fact that you must have a static number of bytes in a buffer.
    Sure I can.
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    char * readline( void )
    {
        int c = 0;
        size_t x = 0;
        char *s = realloc( NULL, x+1 );
        
        while( (c = fgetc(stdin)) != EOF && c != '\n' )
        {
            char *p = realloc( s, x+1 );
            if( p )
            {
                p[x++] = c;
                s = p;
            }
            else
                return "memory allocation failed";
        }
        s[x] = '\0';
    
        return s;
    }
    
    int main( void )
    {
        char *line = NULL;
        
        printf("Type a line and hit enter: ");
        fflush(stdout);
        line = readline();
        
        printf("\'%s\'\n", line );
    
        return 0;
    }
    Now that's not you were talking about exactly, but it illustrates my point. Especially considering that they're actually using C++, in which case you could override the = operator (probably, haven't tried, don't actually care) to do what I've done.


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

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by quzah View Post
    Sure it is.
    No. The pointer doesn't itself is not any kind of dynamic storage.

    Sure I can.
    My point being that the buffer isn't truly dynamic. You cannot just put stuff in it and hope that it will accommodate all the data. It does not.
    You can resize the buffer to accommodate all the data, however. But you still have a static number of bytes in the buffer.

    I do get your point, though. To get an "unlimited" amount of storage in a buffer, you would use dynamic memory and expand on need.
    However, reading directly into a char pointer is undefined behavior and was the point I was trying to make.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 05:59 AM