tags:

views:

182

answers:

8

I am pretty new to C.

I have a string:

char * someString;

If I want the first 5 letters of this string and want to set it to otherString, how would I do it?

+2  A: 
char* someString = "abcdedgh";
char* otherString = 0;

otherString = (char*)malloc(5+1);
memcpy(otherString,someString,5);
otherString[5] = 0;

UPDATE:
Tip: A good way to understand definitions is called the right-left rule (some links at the end):

Start reading from identifier and say aloud => "someString is..."
Now go to right of someString (statement has ended with a semicolon, nothing to say).
Now go left of identifier (* is encountered) => so say "...a pointer to...".
Now go to left of "*" (the keyword char is found) => say "..char".
Done!

So char* someString; => "someString is a pointer to char".

Since a pointer simply points to a certain memory address, it can also be used as the "starting point" for an "array" of characters.

That works with anything .. give it a go:

char* s[2]; //=> s is an array of pointers to char
char** someThing; //=> someThing is a pointer to a pointer to char.
//Note: We look in the brackets first, and then move outward
char (* s)[2]; //=> s is a pointer to an array of char

Some links: How to interpret complex C/C++ declarations and How To Read C Declarations

Liao
I think you should try compiling `char *[] someThing;` and `char []* someThing;`. You want `char *someThing[];` and `char (*someThing)[];` respectively. And that breaks your algorithm to understand definitions.
Alok
//Thanks, you're right about the bad syntax..fixed the code. However, the algorithm still stands, see the update.
Liao
A: 

strcpy, strdup, otherString[5] = '\0'. Some combo of the preceeding.

GregS
Its better too use the n variants of the string manipulation functions. For e.g. strncpy instead of strcpy to prevent buffer overflow exploits.
HeretoLearn
+9  A: 
#include <string.h>
...
char otherString[6]; // note 6, not 5, there's one there for the null terminator
...
strncpy(otherString, someString, 5);
otherString[5] = 0;
pib
@pib: otherString[5] = '\0';
Manav MN
Or `otherString[5] = (char)0;` If you want to be picky about it. Char is an integer type, so compilers won't (or shouldn't) complain about just assigning a raw integer to it.
pib
+1  A: 
strncpy(otherString, someString, 5);

Don't forget to allocate memory for otherString.

Colin
Note that this may result in an unterminated string (if `someString` contains five or more characters).
strager
A: 

You'll need to allocate memory for the new string otherString. In general for a substring of length n, something like this may work for you (don't forget to do bounds checking...)

char *subString(char *someString, int n) 
{
   char *new = malloc(sizeof(char)*n+1);
   strncpy(new, someString, n);
   new[n] = '\0';
   return new;
}

This will return a substring of the first n characters of someString. Make sure you free the memory when you are done with it using free().

Neal
please check the malloc return value
pm100
A: 
#include <stdio.h>
#include <string.h>

int main ()
{
        char someString[]="abcdedgh";
        char otherString[]="00000";
        memcpy (otherString, someString, 5);
        printf ("someString: %s\notherString: %s\n", someString, otherString);
        return 0;
}

You will not need stdio.h if you don't use the printf statement and putting constants in all but the smallest programs is bad form and should be avoided.

gavaletz
You need to set otherString[5] = '\0' as well
Bill Forster
otherstring after the memcpy is not a valid C string as its not null terminated. After the memcpy you need to add otherstring[5] = '\0';
HeretoLearn
Or you can memset(otherstring,'\0',sizeof(otherstring)); before using it.
HeretoLearn
That is true, and this illuminates a good issue. Code flexibility should not come at the expense of simplicity. It could ba as simple as char otherString[]="00000"; and thus the null terminating character is a non-issue. The use of '0' instead of '\0' was what prompted my response in the first place, and then I went and forgot it myself...
gavaletz
A: 

Generalized:

char* subString (const char* input, int offset, int len, char* dest)
{
  int input_len = strlen (input);

  if (offset + len > input_len)
  {
     return NULL;
  }

  strncpy (dest, input + offset, len);
  return dest;
}

char dest[80];
const char* source = "hello world";

if (subString (source, 0, 5, dest))
{
  printf ("%s\n", dest);
}
Dan Olson
A: 

Doing it all in two fell swoops:

char *otherString = strncpy((char*)malloc(6), someString);
otherString[5] = 0;
Steve Emmerson
never do this. you have got to check that the malloc worked.
pm100
@pm100 I happen to agree but no one else was, so I figured it was implied.
Steve Emmerson