Tuesday, July 14, 2009

I cant understand pointers and structures in C programming?

i know the basics but can anyone tell me tricks and basic things to remember for sure. i get confused when it comes to pointers and arrays and there connection and when to use *p and when to use %26amp;p and when not to. can anyone summarize the rules and common mistakes with pointers and structures in C programming. Also string literal basics as well. Thanks.





Whateveryou can explain will definitely help!

I cant understand pointers and structures in C programming?
Homestar is right as far as he goes. He uses a type called "string" which wasn't part of the ansi c language last Og checked - and quite frankly, strings, char arrays, are almost everybody's first introduction to the finer points of pointers vs the object to which the pointer refers. Og like to suggest picking up a copy of K%26amp;R (because it's thin and pretty easy to understand) and maybe suggest you throw references into the mix (references keep you from making 'stupid' mistakes with pointers - things like freeing memory that you shouldn't).





Everybody write this once:





char *myname = "Og";


char[] firstName = "Og";


char[2] alsoFirstName;


strncpy( alsoFirstName, myname, strlen(myname) );





printf( "myname: %s\n", myname ); // s.b. ok


printf( "firstName: %s\n", firstName ); // also ok


printf( "alsoFirstName: %s\n", alsoFirstName ); // oops!





Why does that third one fail? Because the array doesn't actually have space allocated for two characters plus one terminator (\0) symbol.





Note the different forms of initializing a string pointer, as well (all three variables can act like a char const*). In the first case the string (the two letters 'O', 'g' and the terminator) are kept in some data segment that never changes. In the second and third examples, the string is stored on the stack.





Anyway, this is pretty deep topic. Maybe e-mail work better. Barring any real concrete examples just remember: const and %26amp; (reference type) are your friends.
Reply:Structures are a collection variables under a name.Each variable within the structure can be accessed using the '.' mark.


An example would be this





struct profile{


string name;


int age;


}person1;





As you can see i have created a new structure called Profile and a new instance of this structure called person1.Now if i want to store anything about person1 i would do it like this.





person1.name = "Joe Schmoe"





As you can see i use the '.' mark to access the variables within the structure.Hope that helps you.





As for arrays,they are also a collection of variables,however they all have one datatype.To declare an array i do so.





int age[2]





The number within the square brackets indicates i want two integers named age.


To access each part of this array you also use the square brackets.However you must start counting from 0 when you want to access an array.Below is an example of me initializing both these integers.





age[0] = 65;


age[1] = 23;





I hope you understood me clearly ^_^.





Im not very good with pointers so bare with me.


A pointer points to a space in memory,a pointer will store this place in memory as an address.Declaring a pointer is as easy as declaring any other datatype,but you must place an asterisk (*) after the datatype.





e.g int* my_pointer;





Now to use this pointer we must assign it the address of another variable.For example say we had a variable called "age".To point to this variable in memory we must precede its name with the Ampersand sign (%26amp;).





e.g my_pointer = %26amp;age ;





Now if we were to output this pointer,it would give us the memory address of the variable age.





EDIT:Sorry i code in c++ i forgot to not use strings,however you should understand how structures work :)
Reply:OK... three questions in one.





Firstly pointers. Pointers aren't variables - but they do point to them. Pointers are defined using a * between the type and the variable, for example:


int *pointer


or


int* pointer





The latter makes more sense, as you're defining a pointer to an integer, and not an integer. However, it can cause confusion if you define normal integers on the same line, for example:


int *pointer,variable


or


int* pointer,variable


both define pointer as a pointer to an integer, and variable to an integer.





Pointers let you have multiple instances of the same variable. For example:


int i=3; /* initialise an integer */


int *p; /* create an integer pointer */


p=%26amp;i; /* and point it to the address of i */


*p=5; /* set value of integer pointed to by p, to 5


printf("%d %d",i,*p); /* print out value of both variables */





Not too useful, since I could have just used i and not the pointer? True. However, this comes into its own with allocated blocks of memory and (more simply) arrays.





See http://www.cprogramming.com/tutorial/c/l... for more details.





Next, structures. Basically, a group of variables tied together. For example, you could have an address-book with a structure 'addressBookEntry' for each entry, and within the structure you could have a name, address, phone-number, etc - but which could be referenced by a single variable name (or array) so that all the associated data is kept together within the same structure. Like a box.





I'll point you at the same site, next chapter for more info.


http://www.cprogramming.com/tutorial/c/l...





Finally, string literals. These are defined within source-code as the value of a quoted string. For example:


"This is a string literal"





Again, more info at the same site:


http://www.cprogramming.com/tutorial/c/l...

online survey

No comments:

Post a Comment