Thread: arrays

  1. #1
    Registered User
    Join Date
    Jan 2021
    Posts
    30

    arrays

    I have no idea how these output to these numbers and I don't know what they're for, anyone help?

    Code:
    #include <stdio.h>
    
    
    int main() {
       /* my first program in C */
       printf("Hello, World! \n\n");
       printf("hello world 2 \n\n");
       
       
       int int1 = 1;
       int int2 = 2;
       int int3 = (int1 + int2);
       
       printf("1 + 2 = %d \n\n",int3);
       
       double array1 [10] = {2.0, 4.0, 6.0};
       printf ("%d \n\n",array1);
       
       double array2 [25] = {5.0, 2.0, 9.0};
       printf ("%d \n\n",array2);     
       
       return 0;
    }
    Last edited by oods1; 01-24-2021 at 05:36 AM.

  2. #2
    Registered User
    Join Date
    Feb 2019
    Posts
    70
    I am not sure if I understand your question...

  3. #3
    Registered User
    Join Date
    Jan 2021
    Posts
    30
    Quote Originally Posted by Nikosant03 View Post
    I am not sure if I understand your question...
    sorry I typo' d

    how do arrays work, and what are they for?

    Example, how does:

    Code:
       double array1 [10] = {2.0, 4.0, 6.0};   printf ("%d \n\n",array1);
    get to 6356672

  4. #4
    Registered User
    Join Date
    Sep 2020
    Posts
    62
    6356672 was the address of array1. If you want the actual number you have to use the subscript operator, like array1[0], array1[1]...
    I would recommend that you get a (e)book. Just guessing how thinks work will not get you far.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,537
    array = a street.
    array[0] = a house on that street.
    array[1] = the house next door.

    You use arrays when you have to store multiple things of the same type.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Jan 2021
    Posts
    30
    Quote Originally Posted by Salem View Post
    array = a street.
    array[0] = a house on that street.
    array[1] = the house next door.

    You use arrays when you have to store multiple things of the same type.
    oh

    so how would I printf a house?

  7. #7
    Registered User
    Join Date
    Feb 2019
    Posts
    70
    Arrays are used to store data (they are buffers). If you want read the data inside the array you need to read the index of the array. For example:

    Code:
    #include <stdio.h>
    #define array_size 10
    int main() {
    
    
    char my_array[array_size] = {1,2,3,4,5,6,7,8,9,10}; // declare an array of size 10 where each element is one byte (char) in size
    
    
    // print every index of my_array
    for(char i =0 ; i<array_size; i++){
    
    
    printf("my_array index %d is: %d \n",i,my_array[i]);
    }    
        return 0;
    
    
    }
    run the above here

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,537
    > so how would I printf a house?
    One at a time.

    There is one exception...
    Code:
    char sentence[] = "The cat sat on the mat";
    printf("%s\n", sentence);
    A char array (and ONLY a char array) can be printed in a single printf call.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 02-06-2014, 02:39 PM
  2. Replies: 9
    Last Post: 07-11-2013, 10:57 PM
  3. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  4. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 05:31 AM
  5. Replies: 2
    Last Post: 02-23-2004, 06:34 AM

Tags for this Thread