1

Let's say I have a char *str and I want to assign it characters one by time using using pointers and incrementing ?

I've done :

char *str;
char c = 'a';
*str++ = c;

But it doesn't work.

How can I do that ?

10
  • 4
    It doesn't work because str points to nothing determinate. There is no "there" there. Commented Oct 5, 2017 at 15:56
  • 2
    I like the structure of the There is no "there" there. sentence... Commented Oct 5, 2017 at 15:57
  • 2
    "using using pointers" is probably referring a pointer to pointer :D Commented Oct 5, 2017 at 15:59
  • 1
    Are you trying to treat str as an array? If so, you've failed to give it any space. Commented Oct 5, 2017 at 16:00
  • 2
    The question you posted is quite vague: But it doesn't work. How can I do that?. Assigning an appropriate value to str is how it's done. The rest of your code looks basically OK other than keeping track of how many characters you assign versus how much space str might point to. You can use malloc (str = malloc(...);), for example. Commented Oct 5, 2017 at 16:16

5 Answers 5

6

str is just a pointer. It doesn't point anywhere valid (especially not to some memory you could write to). A simple possibility would be to have it point to an array:

char buf[1024] = {0}; // room for 1024 chars (or 1023 + a 0 byte for a string)
char *str = buf;
char c = 'a';
*str++ = c;
Sign up to request clarification or add additional context in comments.

Comments

3

char *str is a pointer to a char (or an array of chars), however, you never assigned it. As has been mentioned earlier a char * basically says "go there" but there is no there there, you never gave it a value. You first need to use malloc to create space to put things in. Here's an example

char *str = malloc(sizeof(char)*10) //allocate space for 10 chars
char c = 'a';
str[0] = c;

no error check was made to malloc which you should do in your own program. You can also do it as such

char str[10];
char c = 'a';
str[0] = c;

however with this method you will be restricted to 10 chars and you cannot change that amount, with the previous method you can use realloc to get more or less space in your array.

Comments

1

But it doesn't work.

char* str;

... is not initialized to anything, therefore dereferencing it is to undefined behaviour. If it where initialized, then in expression *str++ = c; str++ is a post-increment operator, which returns a copy of the pointer whilst incrementing the original. The effect is that the copy points to the previous, and therefore what is pointed to by the previous pointer is assigned c.

To which part that doesn't work are you referring?

EDIT:

As mentioned in one of the comments, a copy is not really returned but the value is increment in place after having been evaluated.

6 Comments

It doesn't "return a copy". The ++ operator modifies the object (in this case the pointer) in place. It evaluates to the value before incrementing.
@FelixPalmen, in the case of builtin types this is true, yes, but I like to conceptualize is by explaining it as for non-builtins
This is C. There are no (user-)overloaded operators.
@FelixPalmen, I realize this (that operator overloading only applies to c++), and that this is a "C" group. I still think that conceptually, thinking of a copy of the previous being returned whilst the original is incremented is helpful, irrespective of language (c/c++). Also, if there is an evaluation prior to the increment, the assigment makes a copy, not? Therefore: char* p2 = p1++; //assignment to lvalue... returns a copy
@FelixPalmen, also char c = *p++; ... to me effectively implies, the lvalue of what is returned by p++, dereferenced. Yes, the original may be evaluated, but the "copy" is returned, as it amounts to an lvalue
|
1

As a variable with automatic storage duration the pointer str has indeterminate value. If even it had the static storage duration its value would be NULL. So you may not use such a pointer to store data.

What you mean can look for example the following way

#include <stdio.h>

int main( void )
{
    char s[11];

    char *p = s;

    while (p != s + sizeof( s ) / sizeof( *s ) - 1 ) *p++ = 'a';
    *p = '\0';

    puts(s);

    return 0;
}

The program output is

aaaaaaaaaa

Here in the program the pointer p of the type char * is initialized by the address of the first character of the array s.

Thus this statement used in the loop

*p++ = 'a';

fills sequentially the array with the character 'a'.

The next example is more interesting

#include <stdio.h>

char * copy_string(char *dsn, const char *src)
{
    for (char *p = dsn; (*p++ = *src++) != '\0'; )
    {
        //  empty body
    }

    return dsn;
}

int main( void )
{
    char *src = "Hi QBl";
    char dsn[7];

    puts(copy_string(dsn, src));

    return 0;
}

The program output is

Hi QBl

Here is a demonstration of a function that copies one character array containing a string into another character array using pointers.

Comments

0

To assign each character of string 1 to that of string 2, an equivalent length should be allocated for the pointer variable of string 2.

#include <stdio.h>
#include <stdlib.h>

int main()
{   
    char *str = "Hello, world!";
    char *p = (char*)malloc(sizeof(char) * strlen(str));
    *p = '\0';
    char *pHead = p;
    printf("*str = %s\n", str);
    printf("*p = %s\n", pHead);

    while(*str != '\0')
    {
        *p = *str;
        str++;
        p++;
    }

    printf("*p = %s\n", pHead);
}

The output is:

*str = Hello, world!
*p = 
*p = Hello, world!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.