views:

166

answers:

5

Im trying to remove the first char of the string and keep the remainder, my current code doesnt compile and im confused on how to fix it.

My code:

char * newStr (char * charBuffer)
{
    int len = strlen(charBuffer);
    int i = 1;
    char v;
    if(charBuffer[0] == 'A' || charBuffer[0] == 'Q'){
        for(i=1;i<len;i++)
            v = v + charBuffer[i];
    }
    v = v + '\0';
    return v;
}

Gcc: "Warning: return makes pointer from integer without a cast"

Also: "char * newStr (char * charBuffer)" needs to remain the same.

A: 

You can simply move your char pointer one character in:

char* newstring = oldstring + 1;
Ian Wetherbee
like this?:charBuffer = charBuffer + 1;return charBuffer;
Dacto
this is not a terribly safe solution. They will both be viewing the same part of memory, so if the first string ever changes, the second will change as well.Worse, if the first is deallocated, the second will seg fault.
Willfulwizard
I assume the user would error check for null strings, etc and use both strings as read-only. If any editing was done to either string, both strings start to become unstable. Yes Dacto, you could do that too.
Ian Wetherbee
+1  A: 

Your function is declared to return a char * and you are returning a char.

Furthermore, why don't you just return a pointer to the second character?

char * newStr (char * charBuffer)
{
   if (charBuffer && (*charBuffer == 'A' || *charBuffer == 'Q')) return charBuffer + 1;
   return charBuffer;
}
Brandon Horsley
This is bad practice. What if the string is dynamically allocated? Perhaps eventually the storage will be freed (starting from the second character). The string should be copied to new storage first.
advait
He can return strdup(charBuffer + 1) if that is a problem, the solution is the same.
Brandon Horsley
@thethimble Those were my thoughts exactly. I'm glad we both commented on different answers, so they all get corrected.
Willfulwizard
This is exactly how the standard C string libraries work, strchr, strstr, etc.
Brandon Horsley
+4  A: 
Erick Robertson
+2  A: 

Well, your description says you want to deal with "strings", but you code deals with char buffers/pointers. The simplest approach to remove the first character for strings would be

const char *newStr(const char *string)
{
    return string+1;
}

but as that doesn't look at all like what your code is doing, you probabaly want something different. For example, if you want to just remove a leading 'A' or 'Q' and then copy the string to a buffer, you want something like

char *newStr(const char *string)
{
    if (string[0] == 'A' || string[0] == 'Q')
        string++;
    return strdup(string);
}
Chris Dodd
A: 

Several of the other answers recommended returning charBuffer + 1. As I noted in my previous comment:

This is bad practice. What if the string is dynamically allocated? Perhaps eventually the storage will be freed (starting from the second character). The string should be copied to new storage first.

Freeing a piece of storage from the middle will result in undefined behavior.

Instead, try the strdup function which will return a duplicate of the given string.

#include <string.h>
#include <stdio.h>

char *newStr(char* charBuffer) {
    if (charBuffer && (charBuffer[0] == 'A' || charBuffer[0] == 'Q'))
        return strdup(charBuffer + 1);
    else 
        return strdup(charBuffer);
}

void main() {
    char a[7] = "Advait";
    char b[5] = "John";
    printf("%s\n",newStr(a));   // Prints "dvait"
    printf("%s\n",newStr(b));   // Prints "John"
}
advait
Not sure why I'm being down-voted. People wrote library functions to help simply common tasks like string manipulations and copying. Moreover, 90% of the time these library functions are more efficient and robust than manual attempts.
advait