stackoverflow1453

Programming&&Games&&Life

Archive for April, 2014

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();

}

Write a program to print out a multidimensional array elements by using pointers and casting

/*
Name:
Copyright:
Author: https://stackoverflow1453.wordpress.com/
Date:  4/9/2014 4:17:44 PM
Description:
*/
 
#include <stdio.h>
 
void printArr(int(*ptr)[4]);
 
int i, k;
 
int main(void){
 
    int multiArray[3][4] = { { 1, 5, 2, 4 }, { 0, 6, 3, 14 }, { 132, 4, 22, 5 } };
 
    int(*point)[4] = multiArray[0];
 
    for (k = 0; k < 3; k++)
    {  /* You cant use printArr(multiArray++); because printArr waits for a pointer to a 4 element array only*/
        printArr(point++);
 
    }
 
    getchar();
 
}
 
void printArr(int(*ptr)[4]){
 
    /*Casting needed here because we can't print what shows a
    pointer to a 4 element array
    We have to cast it to an int for print reasons.
    */
    int *temp = (int *)ptr;
 
    for (i = 0; i < 4; i++)
    {
        printf("%d ", *temp);
        temp++;
    }
 
    puts("\n");
}

Write a program which has a pointer to a pointer

/*
Name:
Copyright:
Author: https://stackoverflow1453.wordpress.com/
Date:  4/9/2014 2:37:38 PM
Description:
*/

#include <stdio.h>

int main(void){

	int i;
	i = 2;
	int *ptr1 = &i;
	int **ptr_To_Ptr = &ptr1;

	printf("%d\n", *ptr1);
	printf("%d\n", **ptr_To_Ptr);

	getchar();

}