answersLogoWhite

0

Can char pointer contain long address in c?

Updated: 8/19/2019
User Avatar

Wiki User

12y ago

Best Answer

What do you mean by 'long address'?

1. If you are asking about 'near' and 'far' pointers, then you should forget them; simply use Huge Memory Model.

2. If you mean the address of a 'long int'-type variable, then yes, with type-cast:

long l;

char *p = (char *)&l;

Note: for generic pointers you can use type void *

User Avatar

Wiki User

12y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Can char pointer contain long address in c?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How many bytes does float double long string and char store?

float usually 4 double usually 8 long is 8 but integer, unlike double string is a pointer to a memory address containing array of chars, so it doesn't have a fixed size and a char is usually 1, but i think its 2 in java


What are the building blocks of datatypes in C language?

The basic data types in C are int, char, float, double and bool, plus void to represent no type. A char is the smallest unit of addressable storage and has a length of 1 byte (the length of a byte is machine-dependent, but is typically 8-bits long). All other types are measured relative to a char but are implementation defined. Use the sizeof() function to determine the actual length. A char may be explicitly modified with the signed or unsigned modifiers (signed char and unsigned char), however these modified types are independent of a plain char. Whether a plain char is signed or unsigned is implementation-defined. All three have the same length (1 byte). An int can be modified in a variety of ways using the short, long, signed and unsigned modifiers. Note that when a modifier is used without a type, an int is implied, thus a long implies a long int while signed implies a signed int. As with char, a plain int is independent of modified int types. short - at least as long as a char int - at least as long as a short long - at least as long as an int long long - at least as long as a long All of the above can be further modified with signed or unsigned modifiers. float - single-precision real, typically two chars in length (16-bits) double - double-precision real, at least as long as a float. The double type can also be modified with long and is at least as long as a double. Real numbers are always signed (the signed/unsigned modifiers cannot be used). All types can be modified with the post-fix * operator to produce a pointer variable of the given type. All pointer types for a given implementation have the same length (e.g., 32-bits on a 32-bit system). Pointers to unknown types are represent by void*. You may take the address of an unknown type, but to perform pointer arithmetic or dereferencing operations you must cast the pointer to an appropriate type. All types can be modified with the suffix operator [] to create an array of that type. All other types are either type aliases (such as size_t meaning unsigned long) or user-defined types (struct or union types).


What is an array of pointers to pointers?

A pointer is a variable that stores value of address of a variable. Since a pointer itself is a variable, it is allocated a memory location.Pointer to pointer means a pointer which points to the address of a pointer. In other words a pointer to a pointer has the address of the address of a variable.We can have pointers to int, and pointers to char, and pointers to any structures we've defined, and in fact pointers to any type in C, it shouldn't come as too much of a surprise that we can have pointers to other pointers. If we're used to thinking about simple pointers, and to keeping clear in our minds the distinction between the pointer itself and what it points to, we should be able to think about pointers to pointers, too, although we'll now have to distinguish between the pointer, what it points to, and what the pointer that it points to points.


What are the advantge of pointer variables?

The main advantage of a pointer is that, unlike an ordinary variable which can only refer to one object in memory (or an array of objects), a pointer can refer to any object in memory and we can change which object it refers to unless it is declared constant (const). This is an extremely powerful feature as it allows us to pass objects to functions by address, rather than by value, thus enabling the pass by reference mechanism. A pointer is a variable just like any other. When we instantiate a pointer, memory is set aside for it and we can store a value in that memory, take its address, perform arithmetic on the value, and so on. Where it differs from an ordinary variable is that the stored value represents a memory address. We call them pointers because they "point to" that memory address and thus a pointer can "refer to" the object stored at that address. [Note that in C, pointers are also known as references. However this usage of the term should not be confused with the C++ usage of the term, where a reference is an alias rather than a variable in its own right]. A pointer's type determines how the object it refers to will be interpreted. Thus when using pointers to refer to other objects, it is important that the pointer's type matches the type of object we wish to refer to. That is, if we want to refer to a char, we use a char pointer (char*), but if we want to refer to an int, we use an int pointer (int*). In some cases we may wish to refer to a multi-byte variable (such as a long int) just as if it were really an array of individual bytes or words (such as char or short). For this we simply use a pointer of the appropriate type (such as char* or short*) and cast the address of the object to that same type. For instance: long int x=42; char* p = (char*) &x; // take the address of x, treat as char* rather than long int* Once we have an address stored in memory (in a pointer variable), we can perform "pointer arithmetic" upon it. This feature is primarily used to navigate through an array of values and, just as we can use the array suffix operator to access the individual elements of an array, we can use the array suffix operator on a pointer. Using the previous example, we can refer to the 3rd char of x as follows: char c = p[2]; Given that p refers to a char, if we increment (or decrement) p, we will advance to the next (or previous) char address. Thus the above can also be written: char c = *(p + 2); Given the choice, the array suffix operator is clearly the better option. However, when traversing an array, the ++ and -- operators can be more convenient: long int x = 42; char* p; = (char*) &x; char c; int i; for (i=0; i<sizeof(long int); ++i) { // ... ++p; // advance to the next char element of x } While pointer arithmetic is useful, the main purpose of pointers is to refer to whole objects and to pass those objects to functions by reference rather than by value. Consider the following: void foo (int x) { // ... } When we pass an actual argument to this function, the value of that argument is assigned to the formal argument named x. Thus x is merely a copy of the actual argument. This means the function can modify the value of x without affecting the value passed by the caller. If we want the function to pass the modified value back to the caller, we would normally use the return value: int foo (int x) { ++x; // modify x return x; // return modified value } This is the recommended method of changing a value via a function, as it allows us a bit more flexibility in terms of what we do with that value: int y = 42; foo (y); // upon return, y is still 42 (the return value is lost). y = foo (y); // upon return, y will be 43 int z; z = foo (y); // upon return, y is still 43, while z is 44 However, the pass by value mechanism does not work for arrays, and with good reason; an array name only refers to the start address of the array. The array type determines how that address is interpreted but the type cannot be used to determine how many elements are actually in the array; the size of an array is not part of its type. When we want to pass an array to a function, there are two ways we can declare the function: void foo (int[], unsigned); void foo (int*, unsigned); Both versions mean exactly the same thing and will produce exactly the same machine code. Note that the unsigned argument is used to allow us to explicitly pass the length of the array. Note that second version declares a pointer type. This means the function accepts the address of an object rather than its value. The same applies to the first version. This should come as no surprise given we've already seen how we can use the array suffix operator on a pointer; an array will implicitly convert to a pointer at the slightest provocation. When we pass the address of an object, the function can refer to that object just as if we'd passed the object itself into the function (rather than just its value). This is what we mean by pass by reference. This means we must take the address of the object we wish to pass: int x = 42; // ... foo (&x, 1); Note that we pass the size as 1 because an object of type int has a length of 1 x sizeof(int) and the compiler already knows that an int has a length of sizeof(int). The size relates to the number of elements of the given type, not the number of bytes (or chars). When we pass an actual array, we do not need to take the address because the name of the array implicitly converts to a pointer: int y[100]; // ... foo (y, 100); Although there's no need to, we can explicitly take the address if we really want to: foo (&y, 100); Alternatively, we could take the address of the first element: foo (&y[0], 100); All three methods are functionally equivalent and will generate the exact same code. Give that we can take the address of the first element, we can also take the address of any element and thus pass a subarray: foo (&y[50], 10); Here we are passing a subarray of 10 elements beginning at address y+50. This means we could also write this call as follows: foo (&(y+50), 10); Having passed an object by reference, the function can operate upon that object directly. This is useful when passing large objects as it takes just one CPU operation to assign an address to a pointer. Copying values larger than the size of a pointer is an expensive operation, so we only do that (explicitly) when we actually need a copy. While arrays explicitly convert to a pointer, user-defined types do not. Thus when passing a struct or union object by reference, we must explicitly take the address of that object, just as we would with any other non-array type: typedef struct st { /* ... */ }; void foo (struct s*); int main (void) { struct st s; // ... foo (&s); // ... return 0; }


How do you use a pointer?

A pointer is a reference to a location in memory that is a primitive type in C and other low-level languages. An example use would be if you wanted to store data at run-time, you would have to allocate memory using the malloc function, an you would access that memory area using a pointer Example (modified from http://www.cplusplus.com/reference/clibrary/cstdlib/malloc): #include <stdio.h> #include <stdlib.h> int main () { int i,n; char * buffer; // Declare a pointer to a char printf ("How long do you want the string? "); scanf ("%d", &i); // Get input from user and store in i // Allocate space for i+1 characters buffer = (char*) malloc ( sizeof( char[i+1] ) ); // buffer should now contains a pointer to this location if (buffer==NULL) exit (1); // Memory allocation failed, abort for (n=0; n<i; n++) buffer[n]=rand()%26+'a'; buffer[i]='\0'; printf ("Random string: %s\n",buffer); free (buffer); // Release allocated memory return 0; }


What is called pointers-c plus plus?

Yes, C++ has pointers, which are references to memory locations. which are variables that store memory addresses, or NULL (zero). If the pointer is non-NULL, the pointer is said to dereference the object (or variable) residing at the stored memory address, which permits indirect access to that object so long as the object remains in scope.


What are the data types provided in c language?

C is not an object oriented programming language. As such there are no class data types in C.


How are pointer different from other variables?

A pointer variable is a variable that contains the memory location of another variable or an array (or anything else in memory). Effectively, it points to another memory location. For standard variables, you define a type, assign a value to that variable or read the value from it, and you can also read the memory location (&n = memory location of n) of the variable. For pointers, you can point them to any variable, even another pointer, and you can get the value of the variable it points to (*p), the location of that variable in memory (p), or the address in memory of the pointer itself (&p). Consider: long n = 65; long *p; p = &n; Results: Type | Name | Location | Value long n 0xA3FF 65 long * p 0x31DF 0xA3FF So p points to n. Now, n = 65 &n = 0xA3FF p = 0xA3FF *p = 65 &p = 0x31DF You may find yourself having to use typecasts frequently when using pointers. Pointers are useful when passing data to functions. For instance, consider the following function: void add(int a, int b, int c) { c = a + b; } The problem here is that the computer copies each variable into a new memory location before passing them to the function as local variables. This function effectively does nothing. However, if you change the function to: void add(int a, int b, int *c) { c = a + b; } and call the function by passing in the location of the variable to the function: add(a,b,&c); then you can modify the variable itself. Pointers are also good for working with arrays: char *c = "Hello World"; int i=0; while (c[i] != 0x00) { cout << c[i]; c++ } //print one letter at a time.


Types of data in turbo C programming?

The primitive data types in C include:[signed|unsigned] char[signed|unsigned] short[signed|unsigned] int[signed|unsigned] long[signed|unsigned] long longfloatdoublelong doubleEnumerations (enum) and arrays may also be considered to be primitive types.


Code to implement memcpy in c?

unsigned char * memcpy(unsigned char * s1, unsigned char * s2, long size) { long ix; s1= (char *)malloc(sizeof(strlen(s2))); for(ix=0; ix < size; ix++) s1[ix] = s2[ix]; return s1; }


What Pointer in Data Structure?

No, pointer is not a data type but a reference to an object. Pointers are used to refer back to an object which can be anything from a large data value or a collection of values or objects.A pointer is a variable and is 4 bytes long because 4 bytes = 32 bits, and all addresses in 32 bit operating systems are 4 bytes long :) , so if you want to store an address somewhere you need 4 bytes. A pointer is just 4 bytes in the memory and in these 4 bytes an address is stored. If you ask the address of an element, like char, int, etc., the address you will get will be the address of the first byte. Only the first byte is saved in the pointer, and then you can manipulate the upcoming bytes.For example you declare a structure of 12 bytes and you name it myStruct.let's say that the address of this structure is the address 0x00400001


Is void a data type in c?

what is void data type Void is an empty data type normally used as a return type in C/C++, C#, Java functions/methods to declare that no value will be return by the function. The another use of void is to declare the pointer in C/C++ whe It is not sure that what data type will be addressed by the pointer. eg: void *p; Here p can hold the address of int or float or char or long int or double.