Changing the address of a pointer question

  • Thread starter Thread starter transgalactic
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around the manipulation of pointers and strings in C and C++, specifically addressing how to change the address of a pointer and the correct usage of string types in both languages.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant questions the method of changing a pointer's address, suggesting that using `&` is necessary, while another participant advises against using `strcpy` with `std::string` in C++.
  • Another participant explains the proper way to handle strings in C, emphasizing the need for a null terminator in character arrays.
  • There is a clarification on how to declare strings in C, with examples provided for both character arrays and string literals.

Areas of Agreement / Disagreement

Participants express differing views on the appropriate methods for handling strings and pointers, indicating that there is no consensus on the best practices for these operations.

Contextual Notes

Limitations include the potential misunderstanding of pointer manipulation and string handling across C and C++, as well as the incomplete exploration of the implications of using `strcpy` with `std::string`.

Who May Find This Useful

Readers interested in C and C++ programming, particularly those learning about pointers, strings, and memory management.

transgalactic
Messages
1,386
Reaction score
0
Code:
string t="rrrrr";
string *p=&t;
strcpy(p, "abc");

i was told that it tells P to point on the data which on the address "abc"

i thought that in order to change the address of a pointer we need to add &

strcpy(&p, "abc");

?

(i know that changing the address into "abc" is not recommended)
 
Last edited:
Technology news on Phys.org
Um, don't do that! string, or more precisely std::string is a C++ standard library string, which has its own various operators like assignment.
Code:
char* strcpy( char *to, const char *from )
is a C (and C++) function that copy the C-style string (null terminated char arrays) pointed to by from to the memory pointed to by to, returning to.

Of course, you should just be using std::string if you're programming in C++.

Code:
#include <cstring>

#include <iostream>
#include <string>

int main()
{
    char *p = new char[ std::strlen("abc") + 1 ]; // Don't forget the space for '\0'
    std::strcpy(p, "abc");
    std::cout << p << '\n';

    std::string t;
    std::string *pt = &t;
    *pt = "def";
    std::cout << t << '\n';
}
 
how do you do a string in C??

an array of chars?

char a[5]={a,b,c,d,e}
like that
??
 
transgalactic said:
how do you do a string in C??
an array of chars?
Yes
char a[5]={a,b,c,d,e}
like that
Either
char a[6]={'a','b','c','d','e','\0'}
You need to mark the end of a string in C with a null value, ie a zero.

or char a[]="abcde"
C will automatically size a one dimensional array for you and if you supply the string value in quotes it will also add the terminating zero.
 
thanks
 

Similar threads

Replies
3
Views
2K
Replies
7
Views
4K
Replies
12
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 17 ·
Replies
17
Views
4K
Replies
7
Views
5K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 11 ·
Replies
11
Views
5K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
6
Views
5K