======Part 1======
=====Entries=====
====September 20, 2011====
*Fourth week of school and I feel like I have gotten nothing done. I am trying to get a couple of systems up and running to take up to campus for Joe O's office. I am testing some harddrives as well, all of this is an hpc project. Keywords are killing me, I have 36 to get done before the end of the month, 12 for each class, and I only have 5. Looking through them is teaching me new things, but thats for the c/c++ class, data structs and sys prog I do not even know where to begin.
====September 21, 2011====
*So after spending almost 12 hours in the lair yesterday, I feel like I might have actually accomplished something. Albeit not nearly enough by my standards. Going through the keywords has been tedious and slow at points but the tedious and slow research is usually where you learn the most. I did a couple of interesting **[[http://lab46.corning-cc.edu/opus/fall2011/kkrauss1/part1#semester_experiments|experiments]]** dealing with floats and pointers. The one dealing with pointers was meant to help others understand them a little easier but it had the added effect of solidifying my basic understanding. I also tested a few dozen hard drives in an effort to get a modicum of organization going. I was able to get two units up and running for joes office which I will take up today. Now its time to tackle some more topics.
====September 26, 2011====
Today was a good day for many reasons. It seems like my C is coming together, though I admit I know there is still a long ways to go. The best part was with my structure pointer experiment which actually proved Joe O. wrong. A very difficult task to say the least. Today I am going to do my best to finish up my keywords and work on some objectives.
====September 26, 2011(later)====
Ok so the sysprog book is a bit rough(though not as rough as the discrete book) but I finally finished enough keywords for the month. Some of them I didnt really grasp, such as bit sets and bit masks. Others were a little easier for me(assuming I am right): inodes and data blocks. Hopefully some of this stuff will become easier to understand as we go on. Most things seem to becoming easier but a few still need work.
=====CPROG Topics=====
====Keyword 1: Standard I/O====
**Standard I/O**(standard input/output) Just as its name implies it deals with inputing information to the terminal, and inputing input from the keyboard
* **STDIN**(Standard Input): Deals with reading whatever text is typed(input)
* **STDOUT**(Standard Output): Prints requested information to the screen(output)
* **STDERR**(standard Error): Is specifically designed for printing error messages(error output)
#include
int main()
{
int number;
printf("Please enter a number: "); //this is standard output
scanf("%d", number); //this is standard input
fprintf(stderr, "this goes to STDERR\n"); //this is standard error(output)
return 0;
}
====Keyword 2: Header files====
Libraries for the C/C++ language are actually made up of two parts. The header files that declare, and the actual library or "archive" that contains the definition. The library("archive") will be discussed later.
The Header files is "included" in a programmers code and allows for easy access to predefined functions. System header files are part of the C/C++ standard library. Local ones are ones defined by the programmers. Most beginning programmers use header files from the C/C++ standard library before they even know exactly what they are.
#include
// This is how you include a standard library header file(system) and is almost always used in some way
#include "myheader.h"
// This is how you include a local header file defined by a user
====Keyword 3: Arithmetic(operators and equations)====
Arithmetic is defined as dealing with the manipulation of numbers. The basic necessity of arithmetic is **operators**. The basic operators are (+), (-), (*), (/), and the less known(%).
*(+) is addition
*(-) is subtraction (addition of a negative number)
*(/) is division
*(*) is multiplication
*(%) modular division. This is the same as division but the remaining(left over) number from standard division is what is used.
**Equations** are simply a combination of operators and numbers
#include
int main()
{
int a = 5, b = 6;
float c = 0;
printf("a = 5, b = 6 \n");
c = a + b; // equation using the (+) addition operator
printf("a + b = %.2f\n", c);
c = a - b; // equation using the (i) subraction operator
printf("a - b = %.2f\n", c);
c = a * b; // equation using the (*) multiplication operator
printf("a * b = %.2f\n", c);
c = (float) a / (float) b; // equation using the (/) division operator
printf("a / b = %.2f\n", c);
c = a % b; // equation using the (/) modular division operator.
printf("a %% b = %.2f\n", c);
return(0);
}
====Keyword 4: Bitwise Operators(and, or, not ,xor)====
As defined by Practical C programming, 3rd edition, an **Operator**: is a symbol that represents an action to be performed.
**Bit operators** allow us to work on the individual bits of a data type. Thus far I have not seen them used too much but it is important to know what they are. It will also make it clear why the equivalent logical operators are symbolized the way they are.
***&** bitwise and
***|** bitwise or
***^** bitwise exclusive(does not have an equivalent logical operator)
***~** complement (does not have an equivalent logical operator)
***<<** shift left (does not have an equivalent logical operator)
***>>** shift right(does not have an equivalent logical operator)
**resources**
*Chapter 11 of Practical C programming, 3rd edition
====Keyword 5: Variables(types,ranges, sizes)====
**Variables** are simply memory locations that are given names and can be assigned values. **Variables** are used to store different **types** of data in memory for future usage.
The two basic variable types are **Numeric** and **character**.
*Numeric has several subtypes and can be integers or real values
*characters are letters, ASCII symbols, and even 0-9
Here is a list of data types and their range. (this list assumes **signed** integers, you can add unsigned to the data type shifting the range, size stays the same)
short Numeric - Integer signed: -32768 to 32767 unsigned: 0 to 4294967295
int Numeric - Integer signed: -2147483648 to 2147483647 unsigned: 0 to 4294967295
long Numeric - Integer signed: -2147483648 to 2147483647 unsigned: 0 to 4294967295
float Numeric - Real signed: 1.2 X 10-38 to 3.4 X 1038 no unsigned
double Numeric - Real signed: 2.2 X 10-308 to 1.8 X 10308 no unsigned
char Character signed: -128 to 127 unsigned: 0 to 255
These values are all relative to the operating system, as is the same for the size. A char is typically one byte but that might not always be the case. Programmers should always use the sizeof() function to determine the size of the variable type they wish to use.
example:
#include
int main()
{
int a = sizeof(int),b = sizeof(short),c=sizeof(char):
printf("int: %d\n", a);
printf("int: %d\n", b);
printf("int: %d\n", c);
return 0
}
====Keyword 6: Strings====
**From wikipedia**: a **string** is a finite sequence of symbols that are chosen from a set or alphabet.
For example: this entire sentence could be considered a string or several strings being printed in a certain order. In the C++ language their is an actual string variable but the C language does not have one. C handles strings by simply using an array of characters(which is really what a string is: an array of characters). In the example code I will declare an array and set it equal to a word, this can be one with pointers as well and will be discussed in the future.
example:
#include
int main()
{
char string[6] = "Hello"; /* array of characters with six elements is declared and set equal to "Hello"
quotes are not stored but needed to store multiple characters. The six element is needed as the string
terminator. arrays will be gone into greater depth in the future. */
printf("%s\n", string); //this is printing the string(array of characters) to the screen
printf("The string is: %s\n", string); //This is printing a literal string and then the stored string(array of characters)
return 0;
}
====Keyword 7: Pointers(address of, assignment, dereferencing)====
***[[http://lab46.corning-cc.edu/opus/fall2011/kkrauss1/part1?pointers_address_of_assignment_dereferencing|Pointers]]** were dealt with in data structures.
====Keyword 8: Version control====
***[[http://lab46.corning-cc.edu/opus/fall2011/kkrauss1/part1#version_control_checkout_commit_update_add_log|Version control]]** was handled in Data structures.
====Keyword 9: Structures====
***[[http://lab46.corning-cc.edu/opus/fall2011/kkrauss1/start#keyword_3structures|Structures]]** were covered in Data structures keywords
====Keyword 10:Scope( local, block, Global, File)====
**Scope** of a variable is the portion of a program in which a variable is known
*Global variables are outside of all functions and can be used by all functions and blocks(everywhere).
*Local variables are inside a block
*A block is a section of code enclosed in braces. {}
*File scoped variables act just like Global variables except they can only interact with the file they were declared in.
====Keyword 11 Type Casting====
**Type casting** is when you make a variable of one data type act like another type for a single operation. To cast a variable name simply use brackets() in front of the variable and include inside them the temporary variable type you wish to use. Here is some example code:
include
int main()
{
int variable = 97;// decarling a variable of int type and giving it value
printf("%d\n", variable);// simply printing the value of variable as it is
printf("%c\n", (char)variable);// casting variable as a character should give the ascii char value of 97
return 0;
}
Here is the output:
lab46:~$ ./a.out
97
a
I would just like to add that with printf() you actually do not need to cast in this particular case but it gets the point across.
====Keyword 12: Arrays====
**[[http://lab46.corning-cc.edu/opus/fall2011/kkrauss1/start#keyword_5arrays|Arrays]]** were covered in Data structs.
=====DATA Topics=====
====Keyword 1: Version control(checkout, commit, update, add, log)====
**Version control** is, simply put, a repository of files. Any change made to the source file is tracked and can be reversed if necessary. Wikipedia is an example of version control, if some one makes an improper change to a page, the original information is not lost it is stored as the previous version therefore making it easy to be restored.
***Checkout**: is when a user opens up a file from the repository and creates a local working copy. A user can typically call any previous revision or the latest.
***commit**: the opposite of checkout, is when the user writes or merges the changes they made in the local copy back to the repository.
***update**: similar to commit but in reverse order, the user merges any changes to the the source file with the local copy.
***add**: this is simply adding a new file to the repository.
***log**: is a log of changes made to a source file along with time stamp and any messages entered when committed.
Very helpful **[[http://en.wikipedia.org/wiki/Version_control|resource]]**.
====Keyword 2: Pointers(address of, assignment, dereferencing)====
**Pointers** are data types whose value refers to another value stored elsewhere. When you declare a **[[http://lab46.corning-cc.edu/opus/fall2011/kkrauss1/part1#variables_types_ranges_sizes|variable]]** that is a pointer you are simply storing an address in that variable that has the data you want. The address that is pointed to is often called the **pointee**.
***assignment**: makes two pointers point to the same pointee.
***dereferencing**: Is storing a value in the **pointee**.
***address of**: the address of a pointer(&pointer) is the address that the pointee address is stored at.
Here is an **[[http://lab46.corning-cc.edu/opus/fall2011/kkrauss1/part1?pointers_address_of_value_at|experiment]]** I did that deals with pointers.
====Keyword 3: Structures====
*In the past if we have used arrays for storing a group of data. **Structures** basically do the same thing but with an added benefit that arrays do not have. Arrays can only store the same data type but with structures you can have a mixed bag of data types.
*To declare a **structure** called "date" that has 3 variables, 2 are int's and 1 is an array of char's:
struct birthDate {
int year, day;
char month[10];
}; // the structure is now declared
*After the structure called "birthDate" has been declared you can now declare as many "birthDate's" as you want.
struct birthDate Karls; //Now we have declared a variable called Karl witch is a structure of the birthDate type.
Karls.year = 1988; //we've set Karls birthyear to 1988(lies)
*You can create as many structs of the birthDate type as you want and set each variable type INSIDE of EACH struct to whatever you wish. It definitely has its advantages over arrays.
**resources**
*Practical c Programming, 3rd edition chapter 12
*[[http://gd.tuwien.ac.at/languages/c/programming-bbrown/c_078.htm|Similar example found on the web]]
====Keyword 4: Structure pointers====
*Now for the infamous **structure pointer**, the cold hearted beast who will torment you until you smash your shiny new monitor but will eventually become your best friend(we aren't quite there yet). Pointers to structures change things a bit and even have their own special operator.
*Using my previous example from **[[http://lab46.corning-cc.edu/opus/fall2011/kkrauss1/start#keyword_3structures|structures]]** lets say we declared a **pointer** to struct birthDate:
struct birthDate *Brad; // we have declared a variable named Brad which POINTS to a structure of the birthDate type
*Dont freak out its almost over, and in reality quite simple, it just needs to be done repeatedly, like anything fun, to really get it.
*So now how would you think to change the variable year?
(*Brad).year = 1999;// this SHOULD work(in theory)
Brad->year =1999;// this WILL work and is known as the structure pointer
* Thats it, all a structure pointer is (->).
**resources**
*Practical c Programming, 3rd edition chapter 12
*[[http://gd.tuwien.ac.at/languages/c/programming-bbrown/c_078.htm|Similar example found on the web]]
====Keyword 5: Arrays====
Much like **structures**, **arrays** are used to manage a large quantity of data. The difference is: an array cannot mix data types. If you have multiple data types you will need to use an array for each. Arrays are declared the same way you declare many other data types except they have an index added to end signified by the []. For example:
void main()
{
char array[10]; //I just declared a ten element array of the data type char
array = "september";// now I have stored 9 characters within the array.
}
*The reason i only stored 9 characters in my array is when dealing with "strings" you need to have a null terminator.
To access individual elements of the array you simply use the index, which typically starts at 0. array[0] = s, array[1] = e, array[2] = p. This is quite handy if you need to call one particular element of an array.
====Keyword 6: Null pointer====
A **null pointer** simply pointers "nowhere". And uninitialized pointer can pointer virtually anywhere(but unknown), but a NULL pointer points nowhere meaning there is no object associated with the pointer. Setting a pointer equal to null is quite easy. Example:
{
char *variable = NULL; // all done, yes it's THAT easy for me, it can be this easy for you too!
}
====Keyword 7: Void pointer====
A **Void pointer** is a way to deal with raw memory. Void basically means the absence of a data type. Void pointers also allow for passing a pointer to an unspecified type.
====Keyword 8: Function pointers====
**Function pointers** are an interesting concept and, at least for me, a difficult one to understand. I only recently learned of them so I am not sure how strong this entry is going to be. In simplest terms(Joe O. is going to hate this) a Function pointer is a pointer to a function; AKA: a pointer that points to the address where the function is at. They can be used when you are not sure which function you wish to call. As my understanding is limited I will simply link to a better written explanation of function pointers.
**[[http://www.newty.de/fpt/intro.html|Function pointers]]**
====Keyword 9: Static allocation vs. dynamic alocation====
**static allocation** is when memory is allocated before a program is executed(compile time)
**Dynamic allocation** is when memory is allocated as needed during run time.
====Keyword 10: Memory allocation, malloc(),new====
**Malloc()** is a function in C that allocates memory and returns the beginning address. **new** is a keyword for c++ and basically does the same thing malloc() does for c.
{
int *variable;//declaring a pointer to an int
variable = malloc(sizeof(int)); //allocating memory for the variable
}
{
int *variable;//declaring pointer
variable = new int;// and again allocating memory for the variable
}
====Keyword 11: memory de-allocation free(),delete====
**free()** and **delete** are the opposite of **malloc()** and **new**
**free()** is a function in c that will de-allocate memory that was previously allocated, so that it can be used if needed. **delete** is a keyword in c++ that will do the same.
====Keyword 12: Linked lists====
**Linked lists** lists is a sequence of nodes and each node points to the next one i the sequence. Linked lists are nice when you do not know how big the sequence(list) is going to be and allows for virtually infinite growth.
=====SYSPROG Topics=====
====Keyword 1: UNIX systems programming====
**Unix systems programming**, in part, is the understanding the role of an OS as well as what it means to write programs that work directly with the operating system.
**sources**
first page of the sysprog book that I took off of Haas' desk.
====Keyword 2: Kernel space====
**Kernel space** is where the kernel, its extensions and many drivers run. The kernel is the core of the operating system.
====Keyword 3: User space====
**User space** is where everything outside of the kernel space is run(everything not related directly the operating system).
====Keyword 4: File access(open, read, write, lseek, close====
**File access** is how you access and manipulate files. To do so you need certain tools:
***Open** is a system call used for creating a connection to a file. If there is an error a -1 will be returned, if everything works according to plan a positive integer will be returned which will be the **file descriptor**.
***read** is a system call used for reading the data in a file, it can be the entire file or a certain amount of bytes.
***close** is a system call that closes the connection to the file
***write** is a system call that copies information from memory to the file
***lseek** is a system call that sets the current position for the file to a defined position.
====Keyword 5: file descriptors====
A **File descriptor** is a connection created between a process and a file when the **open** system call is used.
====Keyword 6:Buffering====
**Buffering** is reading data in large sizes into memory from user space and then processing it in smaller pieces as the user needs it.
====Keyword 7:System call====
A **System call** is a request from a program for a service of the operating system(kernel).
====Keyword 8:File types and file properties====
Everything in unix is a **file**. There are several file types: Directories, regular files, and links. Each file has certain **properties** which can be seen in unix using ls -l(this will tell you the file type as well). The properties are: permissions, owner, group, size of the file, date of last modification and name of the file.
*There are more file types than I listed, here are some good resources:
*http://tldp.org/LDP/intro-linux/html/sect_03_01.html
*http://www.uic.edu/depts/adn/seminars/unix-intro/filemgmt.html#properties
====Keyword 9:Bitset and bit masks====
A **bitset** is when a bit or group of bits are "set" to 1(true) via a bitmask.
A **bit mask** is used for setting bits to 1 or 0, or to keep a certain section of bits.
====Keyword 10:User ID's, Group ID's====
The owner and group of a file are stored as numbers which are the **User ID** and **Group ID**. The superuser always has a value of 0, everyone else gets a non-negative integer.
====Keyword 11:Filesystem structure, inodes and data blocks====
The Unix **Filesystem** from a users viewpoint is a tree structure. It branches out with each directory and new file. An **inode** is a struct that stores the properties of each file. **Data blocks** are where the actual contents of files are kept. **Blocks** are the same size and one file can use as many as is needed.
====Keyword 12:Directories====
A **directories** is simply a file that contains a list of names of files along with a table of inode numbers for those file properties.
====Keyword 13:links====
**links** in Unix are very similar to shortcuts in windows. It is like having multiple phonebook entries in several places. A **link** is simply a special file that points to another file.
=====HPC1 Topics=====
====Keyword 1====
Identification and definition of the chosen keyword. Substitute "keyword" with the actual keyword.
If you wish to aid your definition with a code sample, you can do so by using a wiki **code** block, an example follows:
/*
* Sample code block
*/
#include
int main()
{
return(0);
}
====Keyword 2====
Identification and definition of the chosen keyword. Substitute "keyword" with the actual keyword.
If you want to demonstrate something on the command-line, you can do so as follows:
lab46:~$ cd src
lab46:~/src$ gcc -o hello hello.c
lab46:~/src$ ./hello
Hello, World!
lab46:~/src$
====Keyword 3====
Identification and definition of the chosen keyword. Substitute "keyword" with the actual keyword.
If you wish to aid your definition with a code sample, you can do so by using a wiki **code** block, an example follows:
/*
* Sample code block
*/
#include
int main()
{
return(0);
}
====Keyword 4====
Identification and definition of the chosen keyword. Substitute "keyword" with the actual keyword.
If you want to demonstrate something on the command-line, you can do so as follows:
lab46:~$ cd src
lab46:~/src$ gcc -o hello hello.c
lab46:~/src$ ./hello
Hello, World!
lab46:~/src$
====Keyword 5====
Identification and definition of the chosen keyword. Substitute "keyword" with the actual keyword.
If you wish to aid your definition with a code sample, you can do so by using a wiki **code** block, an example follows:
/*
* Sample code block
*/
#include
int main()
{
return(0);
}
====Keyword 6====
Identification and definition of the chosen keyword. Substitute "keyword" with the actual keyword.
If you want to demonstrate something on the command-line, you can do so as follows:
lab46:~$ cd src
lab46:~/src$ gcc -o hello hello.c
lab46:~/src$ ./hello
Hello, World!
lab46:~/src$
=====CPROG Objectives=====
====Objective ====
*To understand the basic concepts of arrays and pointers.
===Q&A===
**Arrays**
What is an array?
*An array is a set of consecutive memory locations used to store the same type of data.
What are arrays used for?
*Arrays are used for many things but typically they are used for strings and anytime you have a large quantity of values that are all the same data type
How do you declare an array?
*Arrays can be declared like any other variable except the variable name is followed by []. This is called the index and is how you would access any element of the array
**Pointers**
What is a pointer?
*A pointer is simply a data type that instead of storing a value actually stores another address which in turn has the value
What are pointers used for?
*Pointers not only can be used much like arrays but are also easy to use for pass data to a function. As a memory address is easy to pass and a variable is not.
How do you declare a pointer?
*Pointers can be declared like any other variable except the variable name is preceded by *. This tells the compiler the variable is actually pointing to the data type and is not actually the data type. A pointer is its own data type. Pointers do not have an index.
=====DATA Objectives=====
====Objective 1====
**Some common applications for linked lists:**
*filesystem's
*database tables
=====SYSPROG Objectives=====
====Objective 1====
**Concurrency:**
*Concurrency is when several processes are running at the same time and can possibly interact with each other.
*If not handled correctly a processes can be forever waiting for each other to release a resource(deadlock) or can be continually denied a resource(starvation)
=====HPC1 Objectives=====
====Objective 1====
State the course objective; define what that objective entails.
===Method===
State the method you will use for measuring successful academic/intellectual achievement of this objective.
===Measurement===
Follow your method and obtain a measurement. Document the results here.
===Analysis===
Reflect upon your results of the measurement to ascertain your achievement of the particular course objective.
* How did you do?
* Room for improvement?
* Could the measurement process be enhanced to be more effective?
* Do you think this enhancement would be efficient to employ?
* Could the course objective be altered to be more applicable? How would you alter it?
====Objective 2====
State the course objective; define what that objective entails.
===Method===
State the method you will use for measuring successful academic/intellectual achievement of this objective.
===Measurement===
Follow your method and obtain a measurement. Document the results here.
===Analysis===
Reflect upon your results of the measurement to ascertain your achievement of the particular course objective.
* How did you do?
* Room for improvement?
* Could the measurement process be enhanced to be more effective?
* Do you think this enhancement would be efficient to employ?
* Could the course objective be altered to be more applicable? How would you alter it?
====Objective 3====
State the course objective; define what that objective entails.
===Method===
State the method you will use for measuring successful academic/intellectual achievement of this objective.
===Measurement===
Follow your method and obtain a measurement. Document the results here.
===Analysis===
Reflect upon your results of the measurement to ascertain your achievement of the particular course objective.
* How did you do?
* Room for improvement?
* Could the measurement process be enhanced to be more effective?
* Do you think this enhancement would be efficient to employ?
* Could the course objective be altered to be more applicable? How would you alter it?
=====Semester Experiments=====
====Experiment 1====
===Question===
Will C allow for unsigned floats?
When looking up information on the ranges of data types I noticed there was never an unsigned range for floats, so I thought I would look into it. I am going to copy simple code trying to define unsigned floats and then copy the outcome of my attempt to compile
#include
int main()
{
float a = 5.0; // To show the compiler will have no issue declaring a float and setting it equal to a value
unsigned float b = 10.0; // to show the compiler will treat 'unsigned' and 'float' as two separate keywords that do not belong together
// this will be seen in the messages after attempting to compile
printf("float = %f\n unsigned float = %u\n", a, b); // this is to remove other warnings when compiling
return 0;
}
lairstation3:~/Desktop/experiments$ gcc -Wall canfloatsbeunsigned.c
canfloatsbeunsigned.c: In function ‘main’:
canfloatsbeunsigned.c:6: error: both ‘unsigned’ and ‘float’ in declaration specifiers
As you can see it simply won't work, it does not compile. My research leads to a very simple answer. There is no equivalent machine code operations for the cpu to execute(the cpu is not designed to deal with unsigned floats). The url below has discussions on the subject.
===Resources===
http://stackoverflow.com/questions/512022/why-doesnt-c-have-unsigned-floats
====Pointers, address of, value at) ====
===Question===
I have noticed that pointers are often times a hurdle for beginning programmers. A common question I hear is: what is the difference between * and &. Early on in the learning process many people do not know what "varName" is when "char * varName" is declared. I am going to attempt to remedy this.
*I am going to show you some code and the output of that compiled code, pay close attention to the notations before moving onto the conclusion
#include
#include
int main()
{
char *varName;// I declared a variable called varName that is a pointer aka (*) to a char
varName = malloc(sizeof(char)); //Malloc returns an address and sets varName = to it(sizes it as well)
*varName = 'a';// I set *varName(the value AT varName) = 'a'
printf("varName = 0x%x\n",(int) varName); // "varName" is a pointer to a char, meaning whatever address pointer is at, has ANOTHER address stored there
printf ("*varName(the value at pointer) = %c\n", *varName);// the address that is stored in varName has a value stored and this is how you get it
printf("&varName(the address of) = 0x%x\n", (int) &varName);// "varName" is at an address and this is how you get it
return 0;
}
*after compiling and running the code this is what will be seen:
lairstation3:~/Desktop/experiments$ ./a.out
varName = 0x98c5008
*varName(the value at pointer) = a
&varName(the address of) = 0xbfa0b90c
lairstation3:~/Desktop/experiments$
===Conclusions===
*If you read info on the keyword **[[http://lab46.corning-cc.edu/opus/fall2011/kkrauss1/part1#variables_types_ranges_sizes|variables]]** you will see that a variable is simply a memory location that is given a name and can be assigned a value. When you declare a pointer(*) you are simply giving a memory address a name and then storing ANOTHER memory address in it.
*This is why "varName" prints out an address, because an address is what is stored AT the address we named "varName".
*The reason you get a value(a character in my example) when you print "*varName"(the value AT varName) is because we are asking for the data that "varName" POINTS to(varName is a given name for an address, we asked for the information that was stored at the address stored within varName).
* Now you also get an address when you print "&varName"(the address of) but that address is different than "varName". This is because "&varName" gives the actual address that "varName" is stored at(varName is just the name of an address and the & operator simply asks for it).
* Hopefully this is starting to make sense(feedback welcome). If it is try to answer(and understand) this question for yourself: what happens if you print "*&varName"
====Structure pointers====
Recently I got into a debate involving structure pointers. This is to show exactly what a struct pointer is:
#include
#include
struct bin {
char *string;
};
int main()
{
struct bin *joesWrong;// declaring a pointer to a struct of the type bin
joesWrong = malloc(sizeof(struct bin)); //mallocing up the struct
joesWrong->string = malloc(sizeof(char)*20); //these next two lines mean the same
(*joesWrong).string = malloc(sizeof(char)*20);// and will be proven in the next block of code
joesWrong->string = "karl's right\n"; // we will see that the following code works as it should
printf("%s\n", joesWrong->string);
(*joesWrong).string = "Joe's wrong\n"; // this means that x->y and (*x).y mean the same thing
printf("%s\n", (*joesWrong).string);
return 0;
}
This is the output of the code, and as you will see it works as expected, thus showing exactly what a function pointer is. x.(*Y) != x->y
karl's right
Joe's wrong
lab46:~$ ^C
lab46:~$
**[[http://www.youtube.com/watch?v=V3y3QoFnqZc|This]]** is a link for Joe :)