Help understanding a Code

This is a discussion on Help understanding a Code within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream.h> int main() { int i=0, j=0; char s1[40]="string 1", s2[35]="string 2", st[75]; while(s1[i]) { st[i]=s1[i]; ++i; } ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    84

    Help understanding a Code

    Code:
    #include <iostream.h>
    
    int main()
    {
        int i=0, j=0;
        char s1[40]="string 1", s2[35]="string 2", st[75];
        while(s1[i])
        {
            st[i]=s1[i]; ++i;
        }
        while ((*(st+i)=s2[j]) !=0)
        {
            ++i; ++j;
        }
        cout <<st;
    }
    Ok this is the code.

    Code:
        while ((*(st+i)=s2[j]) !=0)
        {
            ++i; ++j;
        }
    In this peace of code i dont understand what the '*' is doing and i dont understand why is it important, but when i remove it the compiler returns an error.

    Can someone tell me pls what is that '*' for??

    Tks

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,673
    Array and pointer notation are closely associated with one another (note: this is not saying that arrays and pointers are the same thing - they're not).

    But
    *( array + index ) is just another way of saying array[index]


    > #include <iostream.h>
    You might want to get a more modern C++ compiler, if these are the header files you're including.
    Modern C++ has
    #include <iostream>
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    18
    It's "de-referencing" a pointer. You have a pointer pointing at a location in memory, to access the thing it is pointing to, you need to de-reference it.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    84
    Tks dudes, i got the idea and ty for the sugestion Salem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 04-10-2010, 11:53 PM
  2. Understanding something in Quake 2 Source Code
    By bengreenwood in forum C Programming
    Replies: 5
    Last Post: 08-05-2009, 02:22 PM
  3. Understanding a Line of Quake 2 Source Code
    By bengreenwood in forum C++ Programming
    Replies: 6
    Last Post: 08-04-2009, 03:15 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21