Posts Tagged "C Programming"
C/C++ pointers
Pointers in C/C++ can sometimes be really confusing specially if you are a beginner or like me more used to Java. Here is a small program I found really useful in understanding c pointers:
#include <stdio.h>
int main() {
int int_var = 5;
int *int_ptr;
int_ptr = &int_var; // Put the address of int_var into int_ptr.
printf("int_ptr = 0x%08x\n", int_ptr);
printf("&int_ptr = 0x%08x\n", &int_ptr);
printf("*int_ptr = 0x%08x\n\n", *int_ptr);
printf("int_var is located at 0x%08x and contains %d\n", &int_var, int_var);
printf("int_ptr is located at 0x%08x, contains 0x%08x, and points to %d\n\n",
&int_ptr, int_ptr, *int_ptr);
}
If you compile and run the code you will get something similar to the following:
int_ptr = 0xbffff834 &int_ptr = 0xbffff830 *int_ptr = 0x00000005 int_var is located at 0xbffff834 and contains 5 int_ptr is located at 0xbffff830, contains 0xbffff834, and points to 5
The address of your variables are probably different from what I have here which is obvious. One thing you should pay attention to is the relationship between the value/address of the pointer and the address of the int variable.
Read MoreC Programming Summary of Operators and Precedence
Following is the list of operators in C. The highest priority operators are listed first.
Operator Operation Evaluated. () parentheses left to right [] square brackets left to right ++ increment right to left -- decrement right to left (type) cast operator right to left * the contents of right to left & the address of right to left - unary minus right to left ~ one's complement right to left ! logical NOT right to left * multiply left to right / divide left to right % remainder (MOD) left to right + add left to right - subtract left to right >> shift right left to right << shift left left to right > is greater than left to right >= greater than or equal to left to right <= less than or equal to left to right < less than left to rightRead More
C Programming and gets function
gets() is a function used by most of the c programing starters, mainly because its convenient to use and you don’t need to implement a loop to go through users input character by character. The downside of gets() is that it can be the source of serious problems in your program mainly caused by the buffer over flows which can be caused by the users input.
For instance if you create an array of size 5 for your input if the user inputs more chars the overflowed data will be written into the memory. If there are existing data in those memory locations they will be overwritten by the input. If you have been using gets and it hasn’t been causing any problems you should consider yourself lucky because either the data hasn’t been overwritten on useful memory sluts or perhaps those memory locations haven’t been accessed.
To solve this problem you can use a more secure input function called fgets() which avoids the buffer overflows which can be caused by gets()
For example:
#include <stdio.h>
int main ()
{
char *inputString[5];
fgets(inputString, sizeof(inputString), stdin);
puts(inputString);
return 0;
}
Read More
C Programming scanf
//The general form of the scanf function is:
n = scanf ("string...", pointers);
If you have just started learning C like me, then you would be using the terminal quite often. similar to using printf to print data into the terminal you can use scanf to retrieve terminal inputs into your C program. The following is a small C code that shows how to retrieve the terminal inputs. One thing you should be careful when using inputs and outputs in c is the type of the data. If you use %d to print or retrieve a character instead of an integer, your program would end up terminating with segmentation faults.
/* scanf example */
#include <stdio.h>
int main ()
{
char inputString [80];
int integer;
printf ("Enter your family name: ");
scanf ("%s",inputString);
printf ("Enter your age: ");
scanf ("%d",&integer);
printf ("Mr. %s , %d years old.\n",inputString,integer);
printf ("Enter a hexadecimal number: ");
scanf ("%x",&integer);
printf ("You have entered %#x (%d).\n",integer,integer);
return 0;
}
Read More
C Pointers and memory addresses
I haven’t done any C programming for a long time now. Recently I have been looking back at C and assembly what has been obvious so far is that there are not much resources on the web as you have for Java and other web based programming languages. It is even much worst in the case of assembly.
Here is a small C program describing how to use char pointers, pass values by reference and analyze memory addresses:
#include <stdio.h>
void printIt(char *name){
int counter=0;
int length = strlen(name);
while(counter<length){
printf("Character %c stored at address %u\n",*name++,name);
counter++;
}
}
int main()
{
char name[100] = "C pointers and memory addresses";
char *pointerToName;
pointerToName = &name;
printf("Starting memory address of name %u \n",name);
printIt(pointerToName);
return 0;
}
Read More