Put string in an array of characters

0

I am learning character array and wanted to do a simple program in which I create a 200x100 character array, put a string at position 0 and print it out.

This is part of a larger program I'm doing, but I need to understand how to insert a string into the array of characters to do.

I know that for the user to insert, I just put gets(x[i]) , but I need to put it in the code.

#include <stdio.h>
#include <conio.h>

int main(){
    char mat[200][100];
    mat[0] = "paulo";
    printf("%s", mat[0]);
    getch();
    return 0;
 }
    
asked by anonymous 18.06.2018 / 19:44

1 answer

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

int main(){
    char mat[200][100];
    strcpy(mat[0], "paulo");
    printf("%s", mat[0]);
 }

You need to manually copy the data from the static area to the stack. Remember that the name can only be up to 99 characters long because of the terminator .

You can use pointers and access content directly in the static area, but it can not be changed there. It is possible to change the pointer that initially pointed to static area and then could point to the stack or heap.

    
18.06.2018 / 19:53