In C, a string is actually stored as an array of characters, so the 'string pointer' is pointing to the first character. For instance,
char myString[] = "This is some text";
You can access any character as a simple char by using myString as an array, thus:
char myChar = myString[6];
printf("%c\n", myChar); // Prints s
Hope this helps! David
Answer from David Oakley on Stack OverflowIn C, a string is actually stored as an array of characters, so the 'string pointer' is pointing to the first character. For instance,
char myString[] = "This is some text";
You can access any character as a simple char by using myString as an array, thus:
char myChar = myString[6];
printf("%c\n", myChar); // Prints s
Hope this helps! David
In C, there's no (real, distinct type of) strings. Every C "string" is an array of chars, zero terminated.
Therefore, to extract a character c at index i from string your_string, just use
char c = your_string[i];
Index is base 0 (first character is your_string[0], second is your_string[1]...).
I know that a string is already technically an array of chars, but when I try to use toupper(string), it doesn’t work because toupper is designed to capitalize chars and not strings, per the documentation. I’ve been making it overly complicated and it’s stressing me out. So to start, I created an “int N=strlen(string);”, then created an array that’s “char upper[N];”. Then I write a for loop written as(please forgive the terrible syntax I’m about to write), “for (int i = 0; i < N; i++) { toupper(upper[j]); }”. What am I doing wrong?
How can we convert an input string in c++ into an array
In C, how can I convert a string to a character array? - Stack Overflow
Convert string to array of strings in C - Stack Overflow
How to convert string to char array in C++? - Stack Overflow
First of all, it already is a character array, NULL-terminated. You can simply choose to make another pointer to it, by saying char *c = argv[1];, or you can copy the entire string (null-terminator and all) into another array using the strcpy(char *destination, char *source)function http://www.cplusplus.com/reference/clibrary/cstring/strcpy/ like this:
char c[BUFFER]; //This has to be big enough to hold length + 1
strcpy(c, argv[1]);
Either approach is valid.
You can treat argv[1] as though it were an array of char (i.e., you can subscript it as argv[1][i]). You can pass it as an argument to any function that doesn't try to modify it (e.g., printf, strtol, strchr, etc.). You cannot write to it, though, so if you need to modify the contents for any reason (either directly or through a function like strtok or strcpy), you'll have to create a local copy and manipulate that.
If you are using C89 or earlier, use this method:
#include <stdlib.h>
#include <string.h>
int main(int argc, char **argv)
{
char *c = NULL;
if (argc >= 2)
{
c = malloc(strlen(argv[1]) + 1);
strcpy(c, argv[1]);
}
/**
* Do stuff with c
*/
free(c);
return 0;
}
If you are using C99, you can use a VLA:
#include <string.h>
int main(int argc, char **argv)
{
if (argc < 2)
return 0;
size_t len = strlen(argv[1]);
char c[len+1];
strcpy(c, argv[1]);
/**
* do stuff with c
*/
return 0;
}
Just remember a few things:
The type of
argvischar **, notchar *[N]; similarly, the type ofargv[i]ischar *, notchar [M]. In most circumstances it doesn't matter; you can use a subscript operator on a pointer as though it were an array (array subscripting is defined in terms of pointer arithmetic), but remember that pointers and arrays are not the same thing and are not always interchangeable;The value of
argv[argc]is alwaysNULL;Except when it is the operand of the
sizeofor unary&operators or is a string literal being used as an initializer in a declaration, an expression of array type is converted ("decays") from "N-element array of T" to "pointer to T", and the value of the expression is the location of the first element;C doesn't have a string data type as such; strings are represented as 0-terminated sequences of
char. String literals such as"hello"are stored in such a way that they exist over the lifetime of the program, and you can access them via a pointer value; attempting to modify them results in undefined behavior (i.e., don't do that).
That's exactly what strtok(3) does. All you need to do is allocate an "array", for example
size_t num = 16;
char **arr = malloc(num * sizeof(char *));
then fill it with the results of strtok() and count ... if you reach num, do something like
if (count == num)
{
num *= 2;
arr = realloc(arr, num * sizeof(char *));
}
add error checking for malloc() and realloc().
If you are concerned about the length of the string,reallocate the size of some dynamically allocated array accordingly.than you can store individual words in another array.check this out :
#include <stdio.h>
#include <string.h>
int GetWords(const char *String,char buffer[][255]);
int main(void)
{
char buffer[30][255];
const char *sentence = "Hello how are you";
int num_of_words = GetWords(sentence,buffer);
for( int n = 0 ; n < num_of_words ; n++ )
{
printf("word %d : %s\n" , n+1 , buffer[n]);
//or do something else
}
return 0;
}
int GetWords(const char *String,char buffer[][255])
{
int x = -1 , y = 0 , z = 0 ;
size_t len = strlen(String) , n = 0 ;
for( n = 0 ; n < len ; n++ )
{
y++;
if( String[n] == ' ' /* || String[n] == ',' */ )
{
y = 0;
z = 0;
}
if( y == 1 ) x++;
if( y > 0 )
{
buffer[x][z] = String[n];
z++;
buffer[x][z] = '\0';
}
}
//return number of words
return (x+1);
}
Simplest way I can think of doing it is:
string temp = "cat";
char tab2[1024];
strcpy(tab2, temp.c_str());
For safety, you might prefer:
string temp = "cat";
char tab2[1024];
strncpy(tab2, temp.c_str(), sizeof(tab2));
tab2[sizeof(tab2) - 1] = 0;
or could be in this fashion:
string temp = "cat";
char * tab2 = new char [temp.length()+1];
strcpy (tab2, temp.c_str());
Ok, i am shocked that no one really gave a good answer, now my turn. There are two cases;
A constant char array is good enough for you so you go with,
const char *array = tmp.c_str();Or you need to modify the char array so constant is not ok, then just go with this
char *array = &tmp[0];
Both of them are just assignment operations and most of the time that is just what you need, if you really need a new copy then follow other fellows answers.