stackoverflow1453

Programming&&Games&&Life

Archive for REF BIG

What is stack? What is heap?

http://stackoverflow.com/questions/79923/what-and-where-are-the-stack-and-heap

http://stackoverflow.com/questions/18193850/whats-the-error-with-fgets-in-this-code?rq=1

What is the difference between stack allocation and heap allocation?

/*

Name:
Copyright: Deitel C How to Program
Author: StackOverflow1453
Date: 9/17/2013 3:54:49 PM
Description: What is the difference between stack allocation and heap allocation?

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

int i;

int main(void){

	/*
	For allocating on the stack, 	use char str[20] and 
	for allocating memory on the heap, use char *str = malloc(20) or 
	char *str = malloc(20*sizeof(char)) 
	*/
	/*name is allocated on stack*/
	char name[100]; //stack allocated already
	char *lastName; //heap allocation will be later. Only pointer is created on stack here
	char *cityNamesStackAllocated[3]={"Ankara", "Istanbul", "Sivas"};    //stack already allocated 
	char *cityNamesHeapAllocated[3];  //heap allocation will be later
	/*
	Here memory is allocated on stack for name
	*/
	puts("Enter your name");
	fgets(name, 99, stdin);

	printf("You entered %s which is allocated on stack.\n", name);

	/*
	Here memory is allocated on heap for lastName
	*/
	puts("Enter your last name:");
	lastName=malloc(99);
	fgets(lastName,99,stdin);

	printf("You entered %s which is allocated on the heap.\n", lastName);

	/*
	Here cityNamesStackAllocated contents are printed which already allocated on stack
	*/
	puts("cityNamesStackAllocated contents are printed which is allocated ons tack before");
	for (i = 0; i < 3; i++)
	{
		printf("%s\n", cityNamesStackAllocated[i]);
	}

	/*
	Here cityNamesHeapAllocated contents are expected from user 
	*/
	puts("Enter 3 cities: ");
	for (i = 0; i < 3; i++)
	{
		cityNamesHeapAllocated[i]=malloc(100); //This lina allocates memory dynamcially on the heap
		fgets(cityNamesHeapAllocated[i], 99, stdin);
	}

	/*
	Here cityNamesHeapAllocated contents are printed 
	*/
	puts("cityNamesHeapAllocated contents are printed which is created on the heap.");
	for (i = 0; i < 3; i++)
	{
		printf("%s", cityNamesHeapAllocated[i]);
	}

	/*
	CAN WE CHANGE CHAR POINTER ALLOCATED ON HEAP? YES
	*/
	/*
	name={"d","o","g","a","n"};
	This is wrong. you can not reinitailize it but you can change the contents.(the other way you can use strcpy())
	*/
	printf("Before change inputted name is %s\n", name);
	name[0]='d';
	name[1]='z';
	printf("After change inputted name is %s\n", name);
	/*
	CAN WE CHANGE CHAR POINTER ALLOCATED ON HEAP? YES
	*/
	printf("Before change lastName is %s\n", lastName);
	lastName="Sever"; //Possible
	printf("After change lastName is %s\n", lastName);
	/*
	CAN WE CHANGE CHAR POINTERS ALLOCATED ON STACK ? YES
	*/
	printf("Before change cityNamesStackAllocated[2] is %s\n", cityNamesStackAllocated[2]);
	cityNamesStackAllocated[2]="Bursa";    //Possible 
	printf("After change cityNamesStackAllocated[2] is %s\n", cityNamesStackAllocated[2]);
	/*
	CAN WE CHANGE CHAR POINTERS ALLOCATED ON HEAP ? YES
	*/
	printf("Before change cityNamesHeapAllocated[0] is %s\n", cityNamesHeapAllocated[0]);
	cityNamesHeapAllocated[0]="Van"; //Possible
	printf("After change cityNamesHeapAllocated[0] is %s\n", cityNamesHeapAllocated[0]);

	getch();

}

Array declaration and initialization example code

/*

Name:
Copyright: Deitel C How to Program
Author: StackOverflow1453
Date: 7/2/2013 9:32:09 AM
Description: array declaration and initialization

*/
#include <stdio.h>
#include <string.h>
int i;

int main(void){

	char string;	//declaring a char variable
	char myArray[6]="Hello"; //declaring and initializing a char array on the stack
	char *names[4]={"Joseph", "Eddy", "Mike", "Michel"}; //declaring names array which consists of pointers to 4 strings
	char *myPtr="World"; //declared a pointer to a string
	char s[50]="Primary"; 
	char *pPtr="Secondary";
	
	string='a';
	printf("%c",string);

	printf("\n%s", myArray);
	/*
	*You can't assign strings to variables in C except in initializations.
	*Use the strcpy() function to change values of string variables in C.
	*You can not change myArray string later in the program.
	*There is one way to do this.
	*Using strcpy in string.h library.
	*You can't assign to an array (except when initializing it in its declaration.)  
	*Instead you have to copy to it. (strcpy does this)
	*
	*/
	strcpy(myArray,"Turan");
	printf("\n%s", myArray);
	/*
	*But you can change individual elements!
	*Below code prints "Turtn" 
	*
	*/
	myArray[3]='t';
	printf("\n%s", myArray);
	/*
	*Or you can put '' and this ends the array
	*Below code prints "Tur"
	*
	*/
	myArray[3]=''; 
	printf("\n%s", myArray);

	/*
	*
	*How to print elements pointed by "names" pointer array declared above?
	*%s format specifier must be used to print 
	*/
	for (i = 0; i < 4; i++)
	{
		printf("\n%s", names[i]); //prints the value which pointers in the array point to 
	}
	/*
	*Below prints "W" because pointer points to the first element
	*/

	printf("\n%c",*myPtr); //prints "W"

	/*
	*We can make myPtr point to another string
	*Below will print A
	*/

	myPtr="Another String";
	printf("\n%c",*myPtr); //prints "A"
	printf("\n%p",myPtr); //prints address of pointer

	/*
	*it' seems you thought printf("%s", array_name) will access the the first element ... 
	*But because of "%s" the printf will print all the content of the pointer's variable till it finds the '' in this pointer's variable..
	*printf function prints whole char array until it encounters ''. Not prints the only first element! as stated below
	*Below code prints "Tur"
	*/

	printf("\n%s", myArray); //prints whole character array "Tur"
	//printf("\n%s", myArray[0]); //this gives error! you can not use %s for printing 1 character
	printf("\n%c", myArray[0]); //this prints "T"  

	printf("\n%s",s);	//prints "Primary"
	printf("\n%s",pPtr); //prints "Seco─▒ndary"
	pPtr=s;  //pPtr is a pointer to char and p is equal to address s so they refer to same string
	printf("\n%s",s); //prints Primary
	printf("\n%s",pPtr); //prints Secondary

	getch();

}

GCC Compiler C Variable Ranges…

Variable Ranges

Promotion Rules & printf , scanf format specifers

Promotion Hierarchy Source: Deitel C How to Program