1

I'm trying to write a functions to modify strings in C. If I have a function like

char *func(char *s){
    char *t=s;
    s++;     //option 1
    t++;     //option 2
    *t='a';  //option 2
    return s;
}

If I do something like [option 1]: s++; I believe it will return the pointer to wherever s is now pointing. If I do [option 2]: t++; and *t='a'; then return s, will it return the address for the first spot of s but with modified contents or will it return the address with the original content?

4
  • 7
    Read a book. There's lots of books on how C works. Commented Mar 26, 2012 at 20:34
  • Your function doesn't return anything, currently, so your question is unclear. Commented Mar 26, 2012 at 20:36
  • Books. The original internet. Commented Mar 26, 2012 at 20:38
  • 1
    One of the options is not possible (see the answers). The others, and more are possible. We haven't got enough information to give you good advice. We need more information from you about what you are tring to do. Commented Mar 26, 2012 at 21:04

4 Answers 4

4
char *func(char *s)

In this code, s is a pointer to a region of memory, that (I assume) represents a string.

s++;

now s points to the next char, in the same region of memory, that (I assume) represents a string.

char *t=s;

Now you have two pointers to that region of memory.

t++;
*t='a';

Now you have changed that region of memory, replacing the second character with an 'a'.

Therefore: if you then return s, you will return a pointer to that same region of memory, which was altered. If you want to return an altered copy of the string, you have to make a copy of the memory first.

char *func(char *s){
    char *t=strdup(s); //duplicates a string
    s++;
    *t='a';
    free(t); //you have to free the memory from strdup at some point
    return s; //returns pointer to second character of unchanged string
 }
Sign up to request clarification or add additional context in comments.

1 Comment

Aa you have increased the pointer t the free bit is undefined.
2

If I do t++; and *t='a'; then return s, will it return the address for the first spot of s but with modified contents or will it return the address with the original content?

I believe your question assumes something like this:

char s[] = "abcde";
char *t = s;
t++;
*t = 'a';

Here you've got one string and two pointers that initially both point to the beginning of the string. At the third line, you modify one of the pointers to point to the next character. At the fourth line, you modify the data at that location. Since s points to the same data, and the data is modified, the string that s points to will change.

printf("s is: %s\n", s");   // s is: aacde
printf("t is: %s\n", t");   // t is: acde

BTW, there's no better way to really learn this stuff than to write little test programs and play with them. A good book will explain how things are supposed to work, but playing with code is the way that you grow to really understand the code and believe what the books tell you.

2 Comments

don't you need char s[] = "abcde" to be able to modify it?
@steabert Yes, you certainly do. Thanks for catching that.
0

In the code:

char *func(char *s)
{
    char *t=s;
}

you are missing a return statement, so garbage is returned.

The variable s is a local copy of whatever you passed to the function. If you write s++ inside the function, you change what the local pointer points at, but don't change the argument.

When you do t++; and *t = 'a';, you are making t point to the second character of the string passed as s, and then assigning the character 'a' to that.

So, if you have:

char *func(char *s)
{
    char *t = s;
    s++;
    t++;
    *t = 'a';
    return s;
}

then the return value will be a pointer to the second character of the original string, which has been modified to contain 'a'.

2 Comments

i thought that you needed a double pointer to point to a pointer. wouldn't this be the same as saying int a = b, and if I change a, b will be changed?
@chikuba: I'm not entirely sure what you're asking. Yes, you would need a double pointer to point to a pointer. But s in the argument list is simply a copy of a pointer, not a pointer to a pointer. Similarly, t is a simple pointer. But *t assigns to part of the string pointed at by s. Writing *t = 'a'; modifies the string, but not the pointer to the string. In particular, if the call is func(&array[4]), then the *t assignment modifies array[5] in the calling function, without changing the &array[4] (which is just as well since that isn't modifiable).
0

What behaviour do you need?

Both returning the initial value of s, and returning the final value of s make sense.

The one option you do not have is doing *t='a' but returning with the original memory unchanged. Memory is changed

Returning the original s is quite common, for example strcpy.

It is also common to return a pointer to 'the next char after the one I just modified' (which is an option you did not offer).

You need to think through, and decide what is the helpful thing for the function to do?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.