The storage size of an int in C is loosely defined, and may be either 2 bytes or, more commonly, 4 bytes.
Whether or not it is defined as const won't affect the size.
Well, uh, const unsigned int and const signed int..
int const *p declares a 'p' pointer, that points to a constant integer
Int: 4 bytes Float: 4 double: 8 char: 1 boolean: 1
//Array Passer //Demonstrates relationship between pointers and arrays #include <iostream> using namespace std; void increase(int* const array, const int NUM_ELEMENTS); void display(const int* const array, const int NUM_ELEMENTS); int main() { cout << "Creating an array of high scores.\n\n"; const int NUM_SCORES = 3; int highScores[NUM_SCORES] = {5000, 3500, 2700}; cout << "Displaying scores using array name as a constant pointer.\n"; cout << *highScores << endl; cout << *(highScores + 1) << endl; cout << *(highScores + 2) << "\n\n"; cout << "Increasing scores by passing array as a constant pointer.\n\n"; increase(highScores, NUM_SCORES); cout << "Displaying scores by passing array as a constant pointer to a constant.\n"; display(highScores, NUM_SCORES); return 0; } void increase(int* const array, const int NUM_ELEMENTS) { for (int i = 0; i < NUM_ELEMENTS; ++i) array [i] += 500; } void display(const int* const array, const int NUM_ELEMENTS) { for (int i = 0; i < NUM_ELEMENTS; ++i) cout << array[i] << endl; }
The sizeof long int is platform-dependent, often 4 bytes or 8 bytes.
in stdio.h:extern int printf (const char *fmt, ...);
int const *p declares a 'p' pointer, that points to a constant integer
Well, uh, const unsigned int and const signed int..
32 bits or 4 bytes and an int is not an address, it is a primitive so it directly access the data without a reference.
Int: 4 bytes Float: 4 double: 8 char: 1 boolean: 1
//Array Passer //Demonstrates relationship between pointers and arrays #include <iostream> using namespace std; void increase(int* const array, const int NUM_ELEMENTS); void display(const int* const array, const int NUM_ELEMENTS); int main() { cout << "Creating an array of high scores.\n\n"; const int NUM_SCORES = 3; int highScores[NUM_SCORES] = {5000, 3500, 2700}; cout << "Displaying scores using array name as a constant pointer.\n"; cout << *highScores << endl; cout << *(highScores + 1) << endl; cout << *(highScores + 2) << "\n\n"; cout << "Increasing scores by passing array as a constant pointer.\n\n"; increase(highScores, NUM_SCORES); cout << "Displaying scores by passing array as a constant pointer to a constant.\n"; display(highScores, NUM_SCORES); return 0; } void increase(int* const array, const int NUM_ELEMENTS) { for (int i = 0; i < NUM_ELEMENTS; ++i) array [i] += 500; } void display(const int* const array, const int NUM_ELEMENTS) { for (int i = 0; i < NUM_ELEMENTS; ++i) cout << array[i] << endl; }
int max (const int a, const int b) { return a>b?a:b; } int maxn (const int a[], const unsigned n) { assert (a!=0 && n!=0); // invalid argument(s)! unsigned i, m; m = a[0]; for (i=1; i<n; ++i) m = max (m, a[i]); return m; }
The sizeof long int is platform-dependent, often 4 bytes or 8 bytes.
#include<iostream> class base { int m_data; public: base(const int data):m_data(data){} base(const base& cpy):m_data(cpy.m_data){} base& operator=(const int rhs){m_data=rhs;return(*this);} base& operator=(const base& rhs){m_data=rhs.m_data;return(*this);} virtual ~base(){} }; class derived { public: derived(const int data):base(data){} derived(const derived& cpy):base(cpy){} derived& operator=(const int rhs){return(base::operator=(rhs));} derived& operator=(const derived& rhs){return(base::operator=(rhs));} virtual ~derived(){} }; int main() { derived d=42; }
Use the following functions: int sum (const int* const a, const unsigned size) { int s = 0; for (unsigned i=0; i<size; ++i) s += a[i]; return s; } double average (const int* const a, const unsigned size) { return (double) sum (a, size) / size; } Example usage: int main () { int[3] a = {74, 42, 64}; printf ("Sum = %d\n", sum (a, 3)); // Sum = 180 printf ("Average = %d\n", average (a, 3)); // Average = 60.0 return 0; }
#include<iostream> struct point { int x; int y; point (const int _x, const int _y): x {_x}, y {_y} {} double distance (const point&) const; }; double point::distance (const point& p) const { int w = x - p.x; int h = y - p.y; return sqrt (h*h + w*w); } std::ostream& operator<< (std::ostream& os, const point& p) { return os << '{' << p.x << ", " << p.y << '}'; } int main() { using namespace std; point a {5,10}; point b {7,2}; double d = a.distance (b); std::cout << "The distance between coordinates " << a << " and " << b << " is " << d << ".\n" << std::endl; }
int comp(const int a1[], const int a2[], const int size) { int i; for(i = 0; i < size; ++i) { if(a1[i] != a2[i]) { return 0; } } return 1; }