stackoverflow1453

Programming&&Games&&Life

Archive for C

How to write a C program in Visual Studio ?

Using Microsoft Visual Studio for Simple C Programs

A free version of Microsoft Visual C++ Express is available in: http://msdn2.microsoft.com/en-us/express/aa700735.aspx

To edit your C program:

  1. From the main menu select File -> New -> Project
  2. In the New Project window:
    Under Project types, select Win32 – Win32 Console Application
    Name your project, and specify a location for your project directory
    Click ‘OK’, then ‘next’
  3. In the Application Wizard:
    Select Console application
    Select Empty project
    Deselect Precompiled header
  4. Once the project has been created, in the window on the left hand side you should see three folders:
    Header Files
    Resource Files
    Source Files
  5. Right-click on Source Files and Select Add-> New Item
    Select Code, and give the file a name
    The default here will be a file with a *.cpp extension (for a C++ file). After creating the file, save it as a *.c file.

To compile and run:

  1. Press the green play button.
  2. By default, you will be running in debug mode and it will run your code and bring up the command window.
    To prevent the command window from closing as soon as the program finishes execution, add the following line to the end of your main function:
    getchar();
    This library function waits for any input key, and will therefore keep your console window open until a key is pressed.

Source: http://www.swarthmore.edu/NatSci/tali/E15/Visual_C.html

Demonstrate pointer to a function with an example

/*
Name:
Copyright:
Author: https://stackoverflow1453.wordpress.com/
Date:  4/9/2014 2:36:33 PM
Description: pointer to a function
*/

#include <stdio.h>
int cube(int x);

int cube(int x){

	return x*x*x;

}

//main method
int main(void){

	//p is a pointer to a function whose argument is an integer and returns an integer

	int(*p) (int m);

	//assign cube function's address to p
	p = cube;

	//call the function by using pointer to a function
	printf("%d", p(9));

	getchar();

}

Write a program which uses dynamic memory allocation and sorts the words entered from the user.

/*
Name:
Copyright:
Author: https://stackoverflow1453.wordpress.com/
Date:  4/9/2014 2:36:33 PM
Description: Write a program which uses dynamic memory allocation and sort words or sentences entered from the user.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void sortArrayPointers(char *p[]);
int getUserInput();
void printContent(char *p[], int  counter);

char *myDynamicArray[25];
char buffer[80];
int i = 0;
int j = 0;
int k = 0;
int count = 0;
int countSort;

void sortArrayPointers(char *p[]){

	char *temp;
	i = 0;
	/*
	temp=a
	a=b
	b=temp
	*/

	for (i = 0; i < countSort; i++)
	{
		for (j = 0; j < countSort - 1; j++)
		{
			//if (strcmp(p[j], p[j + 1])>0)   //strcmp is a useful function which compares the strings.
			if (strcmp(p[j], p[j + 1])>0)
			{
				temp = p[j];
				p[j] = p[j + 1];
				p[j + 1] = temp;
			}

		}
	}

}

//gets the user input
int getUserInput(){

	int count;

	puts("Enter words or a sentence, press enter to end");

	//while ((i < 25) && (gets(buffer) != 0) && (buffer[0] != ''))  ==>this works too with gets

	while ((i < 25) && (fgets(buffer, 80, stdin) != 0) && (buffer[0] != '\n'))  //fgets adds a \n at the end

	{
		//allocate pointers space according to buffer
		if ((myDynamicArray[i] = (char *)malloc(strlen(buffer) + 1)) == NULL)
			return -1;
		//copy buffer to allocated space
		strcpy(myDynamicArray[i++], buffer);

	}

	//print the contents
	count = i;

	return count;

}

//prints the contents of the pointer to character array
void printContent(char *p[], int  counter){

	int k = 0;

	for (k = 0; k < counter; k++)
	{
		printf("%s\n", p[k]);
	}

}

//main method
int main(void){

	/* If you do know the number of variables to be stored beforehand use static allocation like below*/

	int myStaticIntArray[10];

	/* If you do not know how many you will store, you have to count how many user entered and
	dynamically allocate that many int's for the array you created */

	int i = 0;
	int k = 0;
	int count = 0;

	count = getUserInput(myDynamicArray);
	countSort = count;
	puts("Before sort:\n");
	printContent(myDynamicArray, count);

	sortArrayPointers(myDynamicArray);
	puts("After sort:\n");
	printContent(myDynamicArray, count);

	getchar();

}

Write a program by passing an array of pointers to a function to print the strings.

/*
Name:
Copyright:
Author: https://stackoverflow1453.wordpress.com/
Date:  4/9/2014 2:36:33 PM
Description: Write a program byassing an array of pointers to a function.
*/

#include <stdio.h>
void printMyArrayOrPointersToChar(char *p[], int n);

int i;

void printMyArrayOrPointersToChar(char *p[], int n){


	for (i = 0; i < n; i++)
	{
		printf("%s\n", p[i]);
	}

}

int main(void){

	//myArray is an array of 8 pointers to char. 1 st pointer is myArray[0], second element is myArray[1]...
	char *myArray[8] = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };
	char *myArray2[12] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };

	for (i = 0; i < 8; i++)
	{
		printf("%s\n", myArray[i]);
	}

	printMyArrayOrPointersToChar(myArray2, 6);
	getchar();

}

Write a program to demonstrate array of pointers

/*
Name:
Copyright:
Author: https://stackoverflow1453.wordpress.com/
Date:  4/9/2014 2:36:33 PM
Description: Write a program to demonstrate array of pointers
*/

#include <stdio.h>

int main(void){

	int i;

	//myArray is an array of 8 pointers to char. 1 st pointer is myArray[0], second element is myArray[1]...
	char *myArray[8] = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };

	for (i = 0; i < 8; i++)
	{
		printf("%s\n", myArray[i]);
	}
	getchar();

}

Show that in Multidimensional arrays multiArray, multiArray[0] and multiArray[0][0] are same.

/*
Name:
Copyright:
Author: https://stackoverflow1453.wordpress.com/
Date:  4/10/2014 2:46:22 PM
Description: Show that multiArray, multiArray[0] and multiArray[0][0] are same.

http://stackoverflow.com/questions/22983734/multiarray-and-multiarray0-and-multiarray0-same/22984784?iemail=1&noredirect=1#22984784
Above link explains why they are all equal.
*/

#include <stdio.h>

int main(void){

	int a = 5;
	int *x = &a;

	printf("x points to address : %10p\n", x);
	printf("x points to value   : %10d\n", *x);
	printf("x's own address is  : %10p\n\n", (void *)&x);

	int multiArray[3][4] = { { 1, 5, 2, 4 }, { 0, 6, 3, 14 }, { 132, 4, 22, 5 } };

	printf("multiArray points to address : %10p\n", multiArray);
	printf("multiArray points to value   : %10p\n", *multiArray);
	printf("multiArray's own address is  : %10p\n", (void *)&multiArray);

	printf("\nmultiArray[0] points to address : %10p\n", multiArray[0]);
	printf("multiArray[0] points to value   : %10d\n", *multiArray[0]);
	printf("multiArray[0]'s own address is  : %10p\n", (void *)&multiArray[0]);

	printf("\nmultiArray[0][0] 's value is    : %10d\n", multiArray[0][0]);
	printf("multiArray[0][0] does not point any value  \n");
	printf("multiArray[0][0] own address is : %10p\n", &multiArray[0][0]);

	getchar();

}

out

Print the address of the pointer variable (not the address where it is pointing to)

/*
Name:
Copyright:
Author: https://stackoverflow1453.wordpress.com/
Date:  4/10/2014 2:46:22 PM
Description: Print the address of the pointer variable (not the address where it is pointing to)
*/

#include <stdio.h>

int main(void){

	int x = 123;
	int *ptr = &x;
	int **ptr2 = &ptr;

	printf("ptr points to address : %10p\n", ptr);
	printf("ptr points to value   : %10d\n", *ptr);

	//strictly you have to cast the address to a void pointer to get correct address ofthe pointer

	printf("ptr's own address is  : %10p\n", (void *)&ptr);

	printf("\nptr2 points to address: %10p\n", ptr2);
	printf("ptr2 points to value  : %10p\n", *ptr2);
	printf("ptr2's own address is : %10p\n", (void *)&ptr2);
	
	getchar();

}