Thread: string pointer question..

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    string pointer question..

    i get two strings
    a and b
    char *a,*b;
    and later put in them some values
    can i do
    a=b;


    i know that strings are arrays and we cant do this because they are defined as const

    ??

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Yes, you can, but if you should depends on the rest of the program code.

  3. #3
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i know that in arrays we cant do it because the are defined as constant

    so we cant do it in strings

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    i know that strings are arrays
    char* is pointer not array
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    but we can represent an array as a pointer that decays to the first cell.
    so why in arrays we cant do it but in strings we do
    ??

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by transgalactic2 View Post
    but we can represent an array as a pointer that decays to the first cell.
    so why in arrays we cant do it but in strings we do
    ??
    there are situation where arrays are handled as pointers to the first element.

    and there are situations where arrays cannot be handles in a ways pointers can.

    you can assign to pointer, you cannot assign to array

    is array C-string or not - is irrelevant

    you just mixed too approaches to the same object in one statement. This prevents from deciding what part of your statement was wrong
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    what do you mean by "there are situation"

    i remember that on this board where i tried to make a copy of an integer array by doing
    a=b
    and i was told that its impossible because its a constant

    so in this case its the same same case but instead integer type pointer i got chat type pointer

    so i cant see why it should work
    ??

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Well, a and b were never arrays to begin with, you defined them as pointers. The fact that you "gave them values" is a tad nonspecific, because there are situations where a=b is perfectly legal and safe, situations where it is legal and not safe, and situations where it is not legal.

    Arrays are an rvalue in C (like constants are) and it is not legal to associate an array name with another value, any statement that attempts this should result in an error. Pointers as lvalues do not have this restriction. Pointers are values, not objects. For further reading and full understanding you can read C For Smarties: Analyzing Expressions.
    Last edited by whiteflags; 03-15-2009 at 02:32 PM.

  9. #9
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by transgalactic2 View Post
    what do you mean by "there are situation"

    i remember that on this board where i tried to make a copy of an integer array by doing
    a=b
    and i was told that its impossible because its a constant

    so in this case its the same same case but instead integer type pointer i got chat type pointer

    so i cant see why it should work
    ??
    it shouldn't. And I just said so.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  10. #10
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    you cant separate pointers and arrays
    they are the same thing

  11. #11
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    Quote Originally Posted by vart View Post
    it shouldn't. And I just said so.
    so it shouldnt
    thanks

  12. #12
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by transgalactic2 View Post
    you cant separate pointers and arrays
    they are the same thing
    of course you can. And they are not.

    a=b - is valid for pointers and invalid for arrays

    sizeof(a) gives you absolutely different things for pointer and array
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  13. #13
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    so string are arrays and we cant do this

  14. #14
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    actually size of gives us different result if its a pointer on a signature
    because its decays

    other wise its the same thing
    correct?

  15. #15
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    so string are arrays and we cant do this
    C doesn't even have a string type, so as a statement that is wrong and as a question it has no context.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  2. I'm not ask for ENTIRE program, only 1 Question !
    By Th3-SeA in forum C Programming
    Replies: 10
    Last Post: 10-01-2003, 12:33 PM
  3. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM
  4. Next Question...
    By Azmeos in forum C++ Programming
    Replies: 3
    Last Post: 06-06-2003, 02:40 PM