Question:
What are pointers, when declared, intialized to?
sweety
2010-10-06 03:51:33 UTC
a) NULL
b) Newly allocated memory )
c) Nothing. Its random

i am askin because i dont know and this is not my homework or so........
Four answers:
?
2010-10-06 10:24:47 UTC
local pointer variable are initialized to garbage address. global pointers to NULL address. seedhi bat no bakwas.
M3taSpl0it™
2010-10-06 11:08:11 UTC
Edit:

You're answer to A,B,C lies in this article i think:



Initializing Pointers

After a local pointer is declared but before it has been assigned a value, it contains an unknown value. (Global pointers are automatically initialized to null.) Should you try to use the pointer before giving it a valid value, you will probably crash your program—and possibly your computer’s operating system as well—a very nasty type of error!

There is an important convention that most C/C++ programmers follow when working with pointers: A pointer that does not currently point to a valid memory location is given the value null (which is zero). By convention, any pointer that is null implies that it points to nothing and should not be used. However, just because a pointer has a null value does not make it “safe.” The use of null is simply a convention that programmers follow. It is not a rule enforced by the C or C++ languages. For example, if you use a null pointer on the left side of an assignment statement, you still run the risk of crashing your program or operating system. Because a null pointer is assumed to be unused, you can use the null pointer to make many of your pointer routines easier to code and more efficient. For example, you could use a null pointer to mark the end of a pointer array. A routine that accesses that array knows that it has reached the end when it encounters the null value. The search() function shown here illustrates this type of approach.



/* look up a name */

int search(char *p[], char *name)

{

register int t;

for(t=0; p[t]; ++t)

if(!strcmp(p[t], name)) return t;

return -1; /* not found */

}



The for loop inside search() runs until either a match is found or a null pointer is encountered. Assuming the end of the array is marked with a null, the condition controlling the loop fails when it is reached.

C/C++ programmers commonly initialize strings. You saw an example of this in the syntax_error() function in the section “Arrays of Pointers.” Another variation on the initialization theme is the following type of string declaration:

char *p = “hello world”;



As you can see, the pointer p is not an array. The reason this sort of initialization works is because of the way the compiler operates. All C/C++ compilers create what is called a string table, which is used to store the string constants used by the program. Therefore, the preceding declaration statement places the address of hello world, as stored in the string table, into the pointer p. Throughout a program, p can be used like any other string. For example, the following program is perfectly valid:

#include

#include

char *p = “hello world”;

int main(void)

{

register int t;

/* print the string forward and backwards */

printf(p);

for(t=strlen(p)-1; t>-1; t–) printf(”%c”, p[t]);

return 0;

}

In Standard C++, the type of a string literal is technically const char *. But C++ provides an automatic conversion to char *. Thus, the preceding program is still valid. However, this automatic conversion is a deprecated feature, which means that you should not rely upon it for new code. For new programs, you should assume that string literals are constants and the declaration of p in the preceding program should be written like this.

const char *p = “hello world”;
JoelKatz
2010-10-06 10:58:46 UTC
None of the answers are correct. The contents are indeterminate. (That is, it is not possible to determine their value.)



They are not random, as random would imply that they are valid values. A random value would have some chance of comparing equal to NULL and some chance of comparing unequal. An indeterminate value cannot even be compared to NULL at all.



See the C standard, sections 6.7.8 (which explains that the value is "indeterminate") and J2 (which makes clear that the standard does not specify what happens if you attempt to determine the value of a variable whose value is "indeterminate".)



Note that the above assumes the pointer has automatic storage duration. If it has static storage duration, the rules are different.



"If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate." - ISO9899, section 6.7.8



So if you write:

int junk(void) { char *j; if(j==NULL) return 1; else return 0; }

This is simply not guaranteed-legal C. The code attempts to determine the value of a variable whose value is indeterminate. (It is, however, not required to be illegal. A compiler is not required to detect this problem and it can do anything it wants with this code, including building code that crashes, refusing to compile it, or anything else. Some compiler manufacturer could, if they wanted, say that the value will be initialized to NULL, or 0xdeadbeef if they want.)
PatriX
2010-10-07 14:36:49 UTC
in variable we used it to store the value i.e. actual data on other hand pointer is used to store memory reffrencei.e. memory address insted of actual value.



whenever there is need to deal with memory refferance and not with actual value , we should use pointer.

It should be declared before it is going to use in our program in other languages but in c, we need to decleare it in main, like other variable using "*"

eg.

int q,

int *p;

q = 3;

*p = q;



the above statement p will store memory location of q and not 3, suppose q is stored at address 1688 in memory, it meanse p stores 1688.



and *p =3(i.e. if we put prefix * it will show value at the location 1688 i.e. 3)

if u will print &q you will get same value as p as 1688


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...