C array of strings🧵

Поділитися
Вставка
  • Опубліковано 14 жов 2024
  • C array of strings tutorial example explained
    #C #string #array

КОМЕНТАРІ • 43

  • @BroCodez
    @BroCodez  3 роки тому +36

    #include
    #include
    int main()
    {
    char cars[][10] = {"Mustang","Corvette","Camaro"};
    //cars[0] = "Tesla";
    strcpy(cars[0], "Tesla");
    for(int i = 0; i < sizeof(cars)/sizeof(cars[0]); i++)
    {
    printf("%s
    ", cars[i]);
    }
    return 0;
    }

  • @benoitgauthier6089
    @benoitgauthier6089 8 місяців тому +6

    One VERY important point when using strcpy() that really should have been mentionned:
    The destination string should be large enough to accept the source string. Otherwise, it will overflow on other memory adresses and potentially corrupt your program

    • @Aymanne-io6vb
      @Aymanne-io6vb 5 місяців тому

      You can also put space to occupy the string space being replaced if the string to replace another string is not large enough, not a permanent solution though:
      #include
      #include
      int main(){
      char names[][10] = {"Hello", "Cameroon", "Doremi", "colagge"};
      printf("No. of rows: %d
      ", sizeof(names)/sizeof(names[0] ) ) ;
      printf("No. of cols: %d
      ", sizeof(names)/sizeof(names[0] ) ) ;
      strcpy(names[1], "Dora " ); //Changes value of a string array. Put space to occupy the rest of the letters since the initial string is large.
      for(int i = 0; i < sizeof(names)/sizeof(names[0]) ; i++){
      printf(" %s
      ", names[i]);
      }
      /*
      for(int i = 0; i < sizeof(names)/sizeof(names[0]); i++){
      for(int j = 0; j < sizeof(names[0])/sizeof(names[0][0]); j++){
      printf("%c", names[i][j]);
      }
      printf("
      ");
      }
      */
      return 0;
      }

  • @alexiosalexiou3180
    @alexiosalexiou3180 2 роки тому +12

    Thanks, simple, straight to the point, analytical.

  • @mohammedazzan7529
    @mohammedazzan7529 3 роки тому +5

    yours was the simplest to understand thank you.

  • @kevser176
    @kevser176 Рік тому +2

    ı really couldnt get this topic but now everything is clear. thanks you are the best🥺

  • @marbles5590
    @marbles5590 Рік тому +2

    Thank you for this video! It can also be *cars[ ] in order to not have wastage of memory

  • @movieyouser
    @movieyouser Рік тому +4

    Sir, you are the help I needed

  • @minari_0324
    @minari_0324 2 роки тому +5

    damn you made our 30-60 min lecture into 3 mins

  • @sebastienroux1790
    @sebastienroux1790 2 роки тому +4

    Can you replace for example "tesla" with a variable that ="tesla" ?

  • @sumitupadhyay8699
    @sumitupadhyay8699 2 роки тому +3

    Can we use use strings in 1d arrays?

    • @dkp8230
      @dkp8230 Рік тому +2

      I'm pretty late but i think no since a string itself is an array of characters so an array of strings must be a type of 2D array

    • @zraie2455
      @zraie2455 10 місяців тому +2

      A string is a 1D array of characters

    • @ambeikaam3609
      @ambeikaam3609 2 місяці тому

      yepp for example: #include
      #define MAX_LEN 10
      int main(void) {
      char lecturer_name[MAX_LEN] = {'A','n','g','e','l','a' };
      char lecturer_name[MAX_LEN] = "Angela"; //this is a string
      char word[MAX_LEN] = {'h', 'e', 'l', 'l', 'o', '\0'};
      int i = 0;
      while (i < MAX_LEN) {
      printf("%c", lecturer_name[i]);
      i++;
      }
      printf("
      ");
      return 0;
      }

  • @TheLivingCrittles
    @TheLivingCrittles 4 місяці тому

    I dont understand the math for the for loop... sizeof(cars) would be 2, since 0 counts as an index. sizeof(cars[0]) is Tesla, which is 5 characters... so wouldn't you be comparing 0, 1, and 2 against 2/5 which is 0.4? so shouldn't the loop break after the first element? so confused...

  • @moaz_king_1001
    @moaz_king_1001 10 місяців тому

    thanks for help :)

  • @DarkDevilVaqif
    @DarkDevilVaqif Рік тому

    10k subs in a day nice

  • @kevser176
    @kevser176 Рік тому

    ı wish ı can like this video multiple times

  • @bernardev3
    @bernardev3 2 роки тому +1

    THANKS BRO!!!!

  • @umutaydn6184
    @umutaydn6184 Рік тому

    thanks alot!

  • @arkabrian5796
    @arkabrian5796 2 роки тому

    THANK YOU SO MUCH!!!

  • @Artameful
    @Artameful Рік тому

    Thx bro

  • @kyte8131
    @kyte8131 Рік тому +8

    and how the hell do i take input from user?

    • @zkskakksis1514
      @zkskakksis1514 10 місяців тому

      Scanf

    • @shouryadeepbera7114
      @shouryadeepbera7114 10 місяців тому

      ​@@zkskakksis1514causes problem when you take consecutive inputs using a loop.

    • @zkskakksis1514
      @zkskakksis1514 10 місяців тому

      ​@@shouryadeepbera7114what do you mean?

    • @shouryadeepbera7114
      @shouryadeepbera7114 10 місяців тому +2

      @@zkskakksis1514 try taking inputs in a string array using loop, it will cause errors...which can be fixed by clearing the input buffer after each input.

    • @zraie2455
      @zraie2455 10 місяців тому

      @@shouryadeepbera7114
      #include
      #include
      #define NAME_LIST_SIZE 10
      #define MAX_STR_LEN 21
      int main() {
      char name_list[NAME_LIST_SIZE][MAX_STR_LEN];
      printf("Please enter a name: ");
      char user_input[MAX_STR_LEN];
      int i = 0;
      while(i < NAME_LIST_SIZE && fgets(user_input, MAX_STR_LEN, stdin) != NULL) {
      strncpy(name_list[i], user_input, strlen(user_input) - 1);
      printf("%s
      ", name_list[i]);
      i++;
      printf("Please enter a name: ");
      }
      printf("
      Program finish ^_^");
      return 0;
      }
      It's better to use fgets in that case

  • @potatopoo6978
    @potatopoo6978 2 роки тому +1

    Why is mustang not printed?

    • @MRCN49
      @MRCN49 2 роки тому +2

      we changed it to "Tesla" a line below

  • @vlastimirzdravkovic7624
    @vlastimirzdravkovic7624 2 роки тому

    I fucking love you

  • @roronoazoro5672
    @roronoazoro5672 23 дні тому

    can someone tell why did it not print mustang?

    • @Wintre_Hesmith
      @Wintre_Hesmith 23 дні тому

      The 9th line of code, strcpy replaced the first string element "Mustang" with "Tesla" instead

    • @roronoazoro5672
      @roronoazoro5672 23 дні тому

      @@Wintre_Hesmith alr thanks bud

  • @latesttrailers-yb8pr
    @latesttrailers-yb8pr Рік тому

    Mine ain't working

  • @MostaphaQamar
    @MostaphaQamar Рік тому

    dark theme sucks. view is not clear. use light color

  • @stonedcodingtom9097
    @stonedcodingtom9097 2 роки тому

    Thx Bro