The C Noob Returns From His Time Away.....

B

blackneos940

Guest
So basically, how are you guys doin'.....? :3 I was away for a while, just thinkin', Programmin', and doing Karate and Ninjutsu like I always do.... :3 But my issue with C is this: I just can't seem to fully understand Arrays..... :( Here:

Code:
#include <stdio.h>

int main()

{
/* This commented part is the process I don't understand.....   :(

  int marks[] = {35, 45, 55, 65, 75, 85};

  float avg[2];

  avg[0] = 65.5;

  avg[1] = 75.8;

  printf ("%d\n", marks[0]);

  printf ("%d\n", marks[3]);

  printf ("%f\n", avg[0]);

*/

  return 0;

}

I get everything EXCEPT the part which deals with, and Prints the Array..... What's up with that.....?? :( I get "int", "printf", "return 0", etc., etc..... It's the REST I don' get....... :( Thanks for any help, guys..... :)
 


Hi!

I used to teach C, so I will attempt to answer this.
Code:
int i;

int marks[6];
In this example, 'i' is a single integer. "marks" is an object called an array that contains space for six different ints.

In your code:
Code:
int marks[] = {35, 45, 55, 65, 75, 85};
You are creating an array of ints and initializing the array to six integer values. Even though you did not specify the size of the array, the compiler sees six integer initializers, and creates an array of six ints.

I grew up in a single stand alone house. (Like a single int) Here in New York, there may be six houses connected together on a street. (Think of this "street" as an array of six "houses"!) Each house contains a different family (Like "marks", is an array of six ints, containing six different values.)

To access an int in the array, you need to use a "subscript" to access each integer. In the array "marks", the subscript would be 0 to 5. (NOT 1 to 6)

marks[0] would access the first int in the array, marks[1] access the second int in the array, etc... This applies to an array of any type, int float, double, etc...

The printf() function calls, print out an individual int from the array of ints, (The value in marks[0], and marks[3]), or a value from the float array. (avg[0], and avg[1])

There are many tutorials on the C Programming Language on the Net. One of them, on the subject of arrays, can be found here.

EDIT:
You should get yourself a good book on The C Programming Language, preferably not K&R, since it is outdated, and has been superseded by several versions of the standard.
 
Last edited:
I find the New Boston to be very easy to follow.


yeah, yeah, I know he is on Windows...I can't help that. ;)

Oh, and visualizing the data in memory can be useful too.
arrays_v1_202x220.gif


where a is the array, 0-4 are the indexes in the array (positions) and 1,2,4,8,16 are the values at those positions. (Follow what @rstanley said. He can explain better than I)
 
@ryanvade: A Windoze based tutorial? Shame, Shame! ;^)

But seriously folks, Bucky's tutorial was for the most part accurate, but he is not a very good teacher. It is a little hard to present the subject effectively in 7 minutes. His presentation is poor, and he leaves out information that I always presented to my students, such as the internals of the how the compiler handles single and multiple dimension arrays. For instance why the following are the same, and legal code:
Code:
#include <stdio.h>

int main(void)
{
  int ary[] = { 10, 20, 30, 40, 50 };

  printf("ary[2] == %d\n", ary[2]);
  printf("2[ary] == %d\n", 2[ary]);

  return 0;
}

The image you show is also a little misleading. It looks like 'a' is a separate pointer to the array elements. In reality, the array name, 'a' is simply the address of the first element of the array.

A good book is better for self teaching, but not a replacement for taking a course from a qualified instructor. From questions I see posed on the Net, and the quality of the code I see in those postings, qualified instructors, are rare. The same is probably true for other computer languages, and other subjects of computing, for Linux, Windoze, and Macs!

I have thought a lot about trying to get back into teaching, somewhere and sometime in the near future.
 
Hi!

I used to teach C, so I will attempt to answer this.
Code:
int i;

int marks[6];
In this example, 'i' is a single integer. "marks" is an object called an array that contains space for six different ints.

In your code:
Code:
int marks[] = {35, 45, 55, 65, 75, 85};
You are creating an array of ints and initializing the array to six integer values. Even though you did not specify the size of the array, the compiler sees six integer initializers, and creates an array of six ints.

I grew up in a single stand alone house. (Like a single int) Here in New York, there may be six houses connected together on a street. (Think of this "street" as an array of six "houses"!) Each house contains a different family (Like "marks", is an array of six ints, containing six different values.)

To access an int in the array, you need to use a "subscript" to access each integer. In the array "marks", the subscript would be 0 to 5. (NOT 1 to 6)

marks[0] would access the first int in the array, marks[1] access the second int in the array, etc... This applies to an array of any type, int float, double, etc...

The printf() function calls, print out an individual int from the array of ints, (The value in marks[0], and marks[3]), or a value from the float array. (avg[0], and avg[1])

There are many tutorials on the C Programming Language on the Net. One of them, on the subject of arrays, can be found here.

EDIT:
You should get yourself a good book on The C Programming Language, preferably not K&R, since it is outdated, and has been superseded by several versions of the standard.
Ok, then..... :3 I think I understand it a bit better, now!..... :) Thanks!..... :D Also, I checked out those Links you sent me..... :) I downloaded that Linux PDF file, but want a Physical book as well..... Which of those books is more modern.....? :)
 
I find the New Boston to be very easy to follow.


yeah, yeah, I know he is on Windows...I can't help that. ;)

Oh, and visualizing the data in memory can be useful too.
View attachment 2072

where a is the array, 0-4 are the indexes in the array (positions) and 1,2,4,8,16 are the values at those positions. (Follow what @rstanley said. He can explain better than I)
Thank you, good sir..... :3 I too like Bucky..... :) And, although one could do better than Windows, it is, in actuality, a suitable Programming environment..... But only if I'm using FOSS utilities, and available "Tune Up" programs..... :) Yeah, MS could learn more from the FOSS community..... MUCH more....... ;) Also, thanks for the Pic, good sir..... :3 BTW, is it warm where you are.....? I'm in the South, so as you can guess, it feels like New York, in late-Spring here..... :D
 
@ryanvade: A Windoze based tutorial? Shame, Shame! ;^)

But seriously folks, Bucky's tutorial was for the most part accurate, but he is not a very good teacher. It is a little hard to present the subject effectively in 7 minutes. His presentation is poor, and he leaves out information that I always presented to my students, such as the internals of the how the compiler handles single and multiple dimension arrays. For instance why the following are the same, and legal code:
Code:
#include <stdio.h>

int main(void)
{
  int ary[] = { 10, 20, 30, 40, 50 };

  printf("ary[2] == %d\n", ary[2]);
  printf("2[ary] == %d\n", 2[ary]);

  return 0;

}

The image you show is also a little misleading. It looks like 'a' is a separate pointer to the array elements. In reality, the array name, 'a' is simply the address of the first element of the array.

A good book is better for self teaching, but not a replacement for taking a course from a qualified instructor. From questions I see posed on the Net, and the quality of the code I see in those postings, qualified instructors, are rare. The same is probably true for other computer languages, and other subjects of computing, for Linux, Windoze, and Macs!

I have thought a lot about trying to get back into teaching, somewhere and sometime in the near future.


Hey, if you DO get into Teaching again, let me know..... :) It would be REFRESHING to be around a FOSS family member....... :3
 
Ok, then..... :3 I think I understand it a bit better, now!..... :) Thanks!..... :D Also, I checked out those Links you sent me..... :) I downloaded that Linux PDF file, but want a Physical book as well..... Which of those books is more modern.....? :)
That list is a little out of date.

The Book I recommend is "C Primer Plus, 6th edition (2013)" by Stephen Prata. You can purchase it through a local bookstore, or on Amazon. If you search around you might find a better price. Although I have not read this edition, I believe it covers the C99 standard and discusses the C11 standard.
 
That list is a little out of date.

The Book I recommend is "C Primer Plus, 6th edition (2013)" by Stephen Prata. You can purchase it through a local bookstore, or on Amazon. If you search around you might find a better price. Although I have not read this edition, I believe it covers the C99 standard and discusses the C11 standard.
Thank you for the help, good sir...... :3 I was WONDERING why no one had written a modern book on C..... :) Also, I'm drinkin' Lemonade from a Wine Glass..... :3 But I STILL can't run a Windows Program in it..... :<
 
Thank you for the help, good sir...... :3 I was WONDERING why no one had written a modern book on C..... :) Also, I'm drinkin' Lemonade from a Wine Glass..... :3 But I STILL can't run a Windows Program in it..... :<
The latest standard of C was created in 2011. The latest C++ standard was 2014 though most people are still using 2011 for some reason.
https://isocpp.org/std/status
 

Members online


Top