Thread: Strings in C

  1. #1
    Banned
    Join Date
    Apr 2015
    Posts
    596

    Strings in C

    Hi ; sorry in advance for that question but I want to be verified and certained of what I believe.
    Can I say that strings are actually pointer of chars?
    For example in c++/java there data type called String; but in C is char * .. So it's actually pointer to char .. No?


    Another question about increasing const pointer.
    If I do char str[20]="hello" ; then I cant increase str++ for printing every char in the array ye? Because str is a const pointer...am I right?
    Last edited by RyanC; 05-21-2019 at 09:55 AM.

  2. #2
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Quote Originally Posted by RyanC View Post
    Another question about increasing const pointer.
    If I do char str[20]="hello" ; then I cant increase str++ for printing every char in the array ye? Because str is a const pointer...am I right?
    In this case, str is an array, not a pointer. You can't change the "value" of an array like you can with a pointer.

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    A string, in C is an array of chars where the last char is '\0' (NUL char)... Because it is an array, a pointer points to the first element of this array, so the pointer isn't "the string", but a reference to it.

    When you declare an array with fixed size, it's name is a const pointer to the first element (cannot be changed), unless when used in a function argument. In this later case, the array notation is only a sintatic helper, since the argument is, indeed, a pointer:
    Code:
    int f(int buff[30]); // this...
    int f(int *buff); // ... is the same as this
    No array bounds are checked.

    Of course you can make a pointer constant using 'const' keyword:
    Code:
    int f( int * const buff ); // buff cannot be changed inside the function, but the pointed data can.
    int f( const int *buff ); // buff can be changed inside the function, but the pointed data can't.
    int f( const int * const buff ); // neither buff or pointed data can be changed.

  4. #4
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by flp1969 View Post
    A string, in C is an array of chars where the last char is '\0' (NUL char)... Because it is an array, a pointer points to the first element of this array, so the pointer isn't "the string", but a reference to it.

    When you declare an array with fixed size, it's name is a const pointer to the first element (cannot be changed), unless when used in a function argument. In this later case, the array notation is only a sintatic helper, since the argument is, indeed, a pointer:
    Code:
    int f(int buff[30]); // this...
    int f(int *buff); // ... is the same as this
    No array bounds are checked.

    Of course you can make a pointer constant using 'const' keyword:
    Code:
    int f( int * const buff ); // buff cannot be changed inside the function, but the pointed data can.
    int f( const int *buff ); // buff can be changed inside the function, but the pointed data can't.
    int f( const int * const buff ); // neither buff or pointed data can be changed.
    So if I have declared char str[20]="hello" ; and I want to incrase the pointer of the array; can I do this str++? Or it's constant and cant change and do str++?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You really need to review your past questions on the same subject.
    Pointing on string
    Printing the middle string.
    Database of evaluating math equations string.
    Removing subString from String
    How to find strings that match specific pattern
    String Compare manual built
    Copying strings from "file" to an allocated array (char **)
    Comparing two strings in C
    string and char issue
    String

    Nothing you do lends any credibility to the fact that you're making forward progress.
    Some of these were from 4 years ago.

    Round and round and round we go, the same old recipe being rehashed, warmed up, eaten and then thrown up.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-13-2017, 01:29 AM
  2. Replies: 2
    Last Post: 05-16-2012, 06:08 AM
  3. converting c style strings to c++ strings
    By fbplayr78 in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2003, 03:13 AM

Tags for this Thread