char str[] = "SACRED";
strcpy( &str[2], &str[3]);

what the single & do ?? is it a typo of && ??

and what bout the other elements ?? won't the str[3] be repeater teice i.e SARRED ?

10 Years
Discussion Span
Last Post by Dave Sinkula

Not a typo. the & symbol in that statement returns the address of the memory at that location. &str[0] is the address of the first byte of the character array, so &strp[2] is the address of the third byte.

strcpy() function requires two parameters, a pointer (or the address of) to the beginning of the destination buffer and a pointer to the source buffer. In the statement I posted it is going to delete the 3d character by shifting all the remaining characters left one character.


lets assume we start with this: char str[] = "SACRED"; Now create two pointers (you need to know about pointers for this to make any sense to you).

// pointer to destination string
char* p1 = &str[2];
// pointer to source string 
char *p2 = &str[3];

// while not end of string
while( *p2 != 0)
   // copy one character from source string to destination string
   *p1 = *p2;
   // increment destination string
   // increment source string
} // end of while statement
*p1 = 0; // null-terminate the destination string.

At the end of the first iteration of the above loop the string will be "SARRED"
second iteration: "SAREED"
third: "SAREDD"
last: "SARED" // only the null terminator was copied


* what does this do ?? I often get errors like can't covert int* to int etc.


depends on the context. *ptr refers to the byte at the address stored in the variable named ptr. When you get an error like can't covert int* to int it means that something was expecting a pointer and you passed only an integer.


so the pointer should be of int type isn't it as int or char is stored in mem. in like "a" stored at 1120 "c" at 1121. ?? Online guides to pointer are also very comfusing.


You might read this tutorial. Although mostly tecnically accurate you can ignore some of the more technical comments until you understand pointers better. Concentrate on the illustrations that tutorial presents.


ok I read it, so I the char *p stores the value of the memory at which the str[2] is stored ???

like if str[2] = f
and add. of f is 500

so p= 500 ???? is it it ??


This has gotton off-topic so I decided to split it into its own thread.

str is a character array that is located somewhere in memory, exactly where we don't know nor do we really care. We let the compiler and the operating system figure that one out because the exact location can change from one run of the program to another.

But for the sake of arguments lets assume that the beginning of the character array named str is at address location 500. Then if you set a pointer p to point to str[2] then the address stored in the p variable will be 500+2 or 502.


and what is **p ?? sorry but I have been having this prob. for quiet a few weeks now.


**p can be either 1) a pointer to a pointer, or 2) a two dimensional array. Compile and run the below, maybe it will clear it up a bit.

void foo( char **p )
    // allocate some memory
    *p = new char(8);
    // copy something to it
    strcpy(*p, "Hello");


int main()
    char *ptr = NULL; // simple pointer
    cout << ptr << "\n";
    return 0

**p can be either 1) a pointer to a pointer, or 2) a two dimensional array.

I'm generally a bit leary about (2). I understand the implication that the meaning of a two dimensional array here is perhaps a simulated two dimensional array that has been dynamically allocated rather than an actual two dimensional array. Whatever I'm trying to say, it has to do with this.


Q. What value will apointer to a data type will have?

Q. Whats the use of a NULL pointer ?? Won't null pointer assign value 0 to the pointer what does it exactly do ??


>>Whats the use of a NULL pointer ?? Won't null pointer assign value 0 to the pointer
That's correct. It initializes a pointer to a known value which can not be used for anything. Its useful for testing pointers to see if they are valid. Programmers make many mistakes by attempting to use uninitialized pointers and setting them to NULL makes it easy to check that.


The value stored in the pointer variable is the address of the data. Desn't matter what type of data (char, integer, float, double, char array, c++ class, etc).


Yes it will because you didn't declare a variable

char *p = "Hello World";
char c = *p;

In the above, c has the value 'H' which is the first letter of the string pointed to by the pointer p.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.