Skip to main content
gamedev.net
🔒 Locked

Programming education

Started by Jiggold Jan 5, 2001 at 10:56 AM 8 replies 700+ views
Original Post
Jiggold
Jiggold
Quick question. I''m just wondering how many have taken programming courses of some sort and how beneficial you found them. I''m still struggling with a few concepts and I know I need at least one course. I''ve more books than I know what to do with, but I seem to reach an impass in each one. So, this is more or less a survey question. Thanks. --------------------------- Now, THERE''s your crap post.
---------------------------Now, there's your crap post.
crazy-vasey
crazy-vasey
Only course I''ve had was pascal in A-Level computing which was pointless as I didn''t learn anything. Which concepts are you stuck on there''s gotta be at least one person here who can help you.
Jiggold
Jiggold
I can''t exactly remember, it''s been that long since I actually looked at the problem. This shows you how discouraged I''ve become. Arrays and calling them is where I left off. I''ll hafta sift through my notes to remember exactly what the problem is. My primary sources are "C for Dummies (both volumes)" and "The Complete C++ Reference". I cross-referenced the two and found it quite effective. Until I got stuck. Maybe it''s a brain fart, I dunno. I just feel kinda defeated. I need a kick in the pants. Or the head. Any advice on calling arrays, though?

---------------------------
Now, THERE''s your crap post.
---------------------------Now, there's your crap post.
Jesse Chounard
Jesse Chounard
I had the basic courses you''re forced to take on the way to BS in Computer Science, but I found that I learned more from my personal projects and books. (Like many CS students, I didn''t appreciate the material presented in class, and did just enough to get the grade I wanted. The classes might have been more beneficial if I hadn''t been stuck in that mindset.)

What do you mean by calling arrays? Declaring them?

Jesse Chounard
jesse@k-isilabs.com
crazy-vasey
crazy-vasey
What exactly do you mean by calling arrays?

Well I''ll just explain arrays. An array is a series of variables that are generally next to each other in RAM. In XC they are declared like like this

variabletype variablename[sizeofarray];

eg int Array[15]; //declares an array of 15 integers

You can initialize them at declaration like this:

int Array[3] = {1,2,3};

Accessing individual variables in arrays is easy to do :

printf("%d",Array[2]); //would output 2 using array above

The number you put in the square brackets is the index of the variable you wish to access.

You can also create multidimensional arrays. eg

floats array2[2][2];

That creates a 2x2 array of floats. You access it the same way you do with a single dimensional array eg.

array2[1][2]; willl access the value stored at 1,2

You can also have dynamically allocated arrays

int *Pointer; //creates a pointer needed for memory allocation

Pointer = (int*)malloc(5); //creates an array of five integers

The (int*) is a cast that converts mallocs return to an integer value (malloc returns void).






Jesse Chounard
Jesse Chounard
quote:
Original post by crazy-vasey
Pointer = (int*)malloc(5); //creates an array of five integers

The (int*) is a cast that converts mallocs return to an integer value (malloc returns void).



Almost right..

That would allocate 5 bytes. You need:
Pointer = (int*)malloc(5 * sizeof(int));

Or the C++ way:
Pointer = new int[5];

Jesse Chounard
jesse@k-isilabs.com
Jiggold
Jiggold
Thanks for the help. I think the problem I was running into was this: it seemed that the examples the book used were calling on a single interger out of the array with a function. Something like...

cheers()[4];

when the array was originally declared to be 15 intergers long:

array[15]={1,2,3...}

I''m probably not remembering it correctly, and I don''t have the book in front of my right now either. But it seems like that was my problem. Thanks for the encouragment, at any rate. It started to click and I was enjoying it, then I hit this roadblock. Ug.


---------------------------
Now, THERE''s your crap post.
---------------------------Now, there's your crap post.
Jesse Chounard
Jesse Chounard
That code could definitely work, if the function is returning a pointer.

Something like:

  
#include<stdio.h>

int array[20];

int* function(void)
{
int *pointer;
pointer = array;
pointer[2] = 25;
return pointer;
}

int main()
{
int returned;
returned = function()[2];
printf("%d\n", returned);
return 0;
}


I hope that''s not confusing.. I was simply trying to illustrate that a pointer and single-dimensional array are pretty much interchangable.

Jesse Chounard
crazy-vasey
crazy-vasey
oops I make that mistake all the time.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.