Вопрос по string, c, arrays – Как мне создать массив строк в C?

221

Error: User Rate Limit Exceeded

char (*a[2])[14];
a[0]="blah";
a[1]="hmm";

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceededprintf(a[1]);Error: User Rate Limit Exceeded

Error: User Rate Limit Exceededchar *strError: User Rate Limit Exceededchar* strError: User Rate Limit Exceeded Mark K Cowan
Error: User Rate Limit Exceeded fortran
Error: User Rate Limit Exceeded Ac3_DeXt3R
Error: User Rate Limit Exceededchar (*a[2])[14]Error: User Rate Limit ExceededaError: User Rate Limit Exceeded Mark K Cowan
Error: User Rate Limit Exceededchar (*a[2])[14]Error: User Rate Limit Exceeded avakar

Ваш Ответ

14   ответов
199

Error: User Rate Limit Exceeded

const char *a[2];
a[0] = "blah";
a[1] = "hmm";

Error: User Rate Limit Exceededconst charError: User Rate Limit Exceeded"blah"Error: User Rate Limit Exceeded"hmm".

Error: User Rate Limit Exceeded

char a[2][14];
strcpy(a[0], "blah");
strcpy(a[1], "hmm");

Error: User Rate Limit ExceededcharError: User Rate Limit Exceeded

0

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

#define INIT_STRING_ARRAY(...)          \
    {                                   \
        char* args[] = __VA_ARGS__;     \
        ev = args;                      \
        count = _countof(args);         \
    }

void InitEnumIfAny(String& key, CMFCPropertyGridProperty* item)
{
    USES_CONVERSION;
    char** ev = nullptr;
    int count = 0;

    if( key.Compare("horizontal_alignment") )
        INIT_STRING_ARRAY( { "top", "bottom" } )

    if (key.Compare("boolean"))
        INIT_STRING_ARRAY( { "yes", "no" } )

    if( ev == nullptr )
        return;

    for( int i = 0; i < count; i++)
        item->AddOption(A2T(ev[i]));

    item->AllowEdit(FALSE);
}

char** evError: User Rate Limit Exceeded_countofError: User Rate Limit Exceededsizeof(arr) / sizeof(arr[0])).

Error: User Rate Limit ExceededA2TError: User Rate Limit Exceeded

3

Error: User Rate Limit Exceeded

char* a[size];

Error: User Rate Limit Exceeded

char a[size1][size2];

Error: User Rate Limit Exceeded

Error: User Rate Limit ExceededError: User Rate Limit ExceededError: User Rate Limit ExceededError: User Rate Limit Exceeded

снимаю шляпу за ваш другой подход ... Такие люди, как вы, делают стек переполнением ...
80

Error: User Rate Limit Exceeded

const char *strings[] = {"one","two","three"};

Error: User Rate Limit Exceeded

Error: User Rate Limit ExceededError: User Rate Limit ExceededError: User Rate Limit ExceededError: User Rate Limit ExceededError: User Rate Limit Exceeded

char arr[MAX_NUMBER_STRINGS][MAX_STRING_SIZE]; 
strcpy(arr[0], "blah");

Update: Error: User Rate Limit ExceededError: User Rate Limit ExceededstrlcpyError: User Rate Limit Exceeded

Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
strcpy_s и другие & quot; безопасные развлечения, ctions & quot; стандартизированы как ISO / IEC TR 24731 (он является опубликованным стандартом ISO и как таковой не доступен онлайн; самый последний проектopen-std.org/jtc1/sc22/wg14/www/docs/n1225.pdf)
strcpy_s - это функция Microsoft. Вероятно, следует избегать, потому что это не в стандартном C.
1

Error: User Rate Limit Exceeded

 char arr[nb_of_string][max_string_length]; 
 strcpy(arr[0], "word");

Error: User Rate Limit Exceeded

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


int main(int argc, char *argv[]){

int i, j, k;

// to set you array
//const arr[nb_of_string][max_string_length]
char array[3][100];

char temp[100];
char word[100];

for (i = 0; i < 3; i++){
    printf("type word %d : ",i+1);
    scanf("%s", word);
    strcpy(array[i], word);
}

for (k=0; k<3-1; k++){
    for (i=0; i<3-1; i++)
    {
        for (j=0; j<strlen(array[i]); j++)
        {
            // if a letter ascii code is bigger we swap values
            if (array[i][j] > array[i+1][j])
            {
                strcpy(temp, array[i+1]);
                strcpy(array[i+1], array[i]);
                strcpy(array[i], temp);

                j = 999;
            }

            // if a letter ascii code is smaller we stop
            if (array[i][j] < array[i+1][j])
            {
                    j = 999;
            }

        }
    }
}

for (i=0; i<3; i++)
{
    printf("%s\n",array[i]);
}

return 0;
}
0
char name[10][10]
int i,j,n;//here "n" is number of enteries
printf("\nEnter size of array = ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
    for(j=0;j<1;j++)
    {
        printf("\nEnter name = ");
        scanf("%s",&name[i]);
    }
}
//printing the data
for(i=0;i<n;i++)
{
    for(j=0;j<1;j++)
    {
        printf("%d\t|\t%s\t|\t%s",rollno[i][j],name[i],sex[i]);
    }
    printf("\n");
}

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
9

Error: User Rate Limit Exceeded

char *strings[]={ "one", "two", "three", NULL };

int i=0;
while(strings[i]) {
  printf("%s\n", strings[i]);
  //do something
  i++;
};
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
-6

Error: User Rate Limit Exceeded

#include <stdio.h>
typedef char string[]
int main() {
    string test = "string";
    return 0;
}

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded;Error: User Rate Limit Exceededstrings?
153

Error: User Rate Limit Exceeded

char strs[NUMBER_OF_STRINGS][STRING_LENGTH+1];
...
strcpy(strs[0], aString); // where aString is either an array or pointer to char
strcpy(strs[1], "foo");

Error: User Rate Limit Exceeded

char strs[NUMBER_OF_STRINGS][STRING_LENGTH+1] = {"foo", "bar", "bletch", ...};

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

char *strs[NUMBER_OF_STRINGS];

Error: User Rate Limit Exceeded

char *strs[NUMBER_OF_STRINGS] = {"foo", "bar", "bletch", ...};

Error: User Rate Limit Exceeded

strs[i] = "bar";
strs[i] = "foo"; 

Error: User Rate Limit Exceeded

strs[i] = "bar";
strcpy(strs[i], "foo");

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

strs[i] = malloc(strlen("foo") + 1);
strcpy(strs[i], "foo");

Error: User Rate Limit Exceeded

char (*a[2])[14];

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded
Error: User Rate Limit ExceededmallocError: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit ExceededT [N]Error: User Rate Limit ExceededT *Error: User Rate Limit Exceededstr = "foo"Error: User Rate Limit Exceeded"foo"Error: User Rate Limit ExceededstrError: User Rate Limit Exceededthis answerError: User Rate Limit Exceeded
Должны ли вы освободить strs [i], когда закончите с этим?
11

Error: User Rate Limit Exceeded

char a1[][14] = { "blah", "hmm" };
char* a2[] = { "blah", "hmm" };
char (*a3[])[] = { &"blah", &"hmm" };  // only since you brought up the syntax -

printf(a1[0]); // prints blah
printf(a2[0]); // prints blah
printf(*a3[0]); // prints blah

Error: User Rate Limit Exceeded

a2[0] = "hmm";
a2[1] = "blah";

Error: User Rate Limit Exceeded

a3[0] = &"hmm";
a3[1] = &"blah";

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

10

Error: User Rate Limit Exceeded

typedef struct name
{
   char name[100]; // 100 character array
}name;

main()
{
   name yourString[10]; // 10 strings
   printf("Enter something\n:);
   scanf("%s",yourString[0].name);
   scanf("%s",yourString[1].name);
   // maybe put a for loop and a few print ststements to simplify code
   // this is just for example 
 }

Error: User Rate Limit Exceededstrcpy;

8

Error: User Rate Limit Exceededconst char *Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

const char *a[2] = {"blah", "hmm"};

Error: User Rate Limit Exceeded

10

Error: User Rate Limit Exceeded

char* strings[3];
strings[0] = "foo";
strings[1] = "bar";
strings[2] = "baz";
PleaseError: User Rate Limit Exceeded*Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceededchar* foo, bar;Error: User Rate Limit Exceededbar?
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
8

Error: User Rate Limit Exceeded

const char *my_array[] = {"eenie","meenie","miney"};

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Похожие вопросы