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 ?

Recommended Answers

All 26 Replies

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.

can you give me the dry run plz.

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
   ++p1;
   // increment source string
   **p2;
} // 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
    foo(&ptr);
    cout << ptr << "\n";
    return 0

// increment source string
**p2;

In some cases it can even be a typo!

**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.

Dave: I agree with that link. What I had in mind was that each array dimension is allocated dynamically instead of statically.

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.

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

A number of strange and interesting things about null pointers are mentioned here:
http://c-faq.com/null/index.html

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

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).

char = *p won't thst generate a error.

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.

What if I want c to give e or l ?? point using an array ?

Now your talking gibberish. Please explain what you mean.

You can use following:

char *p = "Hello World";
char c = *(p + 1);  // 'e'
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.