Thread: syntax question

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    110

    syntax question

    I using a library and in it there are some lines I don't understand. What does this syntax do?
    Code:
    //array of ints
    //index is an integer
     (*array)[index]

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If "array" is really an array of ints, then what you have isn't anything. For this to work, *array should be of pointer or array type (and therefore array itself needs to be pointer-to-array or pointer-to-pointer or array-of-pointer or array-of-array).

  3. #3
    Registered User
    Join Date
    Dec 2013
    Posts
    9
    I wrote a simple program to show what is going on. Just remember to always ask yourself "What do I have and what is it that I want?" I don't know what "array" is in your program. What it's doing depends on what "array" is. This example is to clarify the notation for you.

    Code:
    #include <iostream>
    
    
    int main(int argc, char* argv[]){
        
        int *a[7];
        int index = 4;
        (*a)[0] = 0;
        (*a)[1] = 1;
        (*a)[2] = 2;
        (*a)[3] = 3;
        (*a)[4] = 4;
        (*a)[5] = 5;
        (*a)[6] = 6;
    
        std::cout << "fourth element: " << (*a)[index] << std::endl;
        std::cout << "fourth element: " << *((*a)+4) << std::endl;
    
        return 0;
    }
    Last edited by Meiryousa; 01-15-2014 at 01:18 PM.

  4. #4
    Registered User
    Join Date
    Mar 2012
    Posts
    110
    OK, I got it. Thanks Meiyrusa!

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Meiryousa: your example program is problematic. *a is the first pointer in the array, i.e., a[0]. (*a)[0] thus dereferences that pointer (equivalently a[0][0]), but the pointer was not given a value. Next, (*a)[1] treats that first pointer as if it were a pointer to the first element of an array, and then results in the second element of that array.

    EDIT:
    You could fix your example if you changed your declaration of a to something like:
    Code:
    int numbers[7];
    int (*a)[7] = &numbers;
    Last edited by laserlight; 01-15-2014 at 01:05 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Dec 2013
    Posts
    9
    laserlight:I was using an array of references and putting solid values in them rather than references. It isn't as much problematic as it is not making any sense. At least as far as I understand. Perhaps this would be a better example? Unless I am misunderstanding something.

    Code:
    #include <iostream>
    
    int main(int argc, char* argv[]){
        
        int *a[2]; //a is a reference each element is a reference.
        int a2[7]; //a2 is a reference each element is a value.
        int a3[4]; //same as a2
        int index = 1;
    
        a2[0] = 0;        a3[0] = 7;
        a2[1] = 1;        a3[1] = 8;
        a2[2] = 2;        a3[2] = 9;
        a2[3] = 3;        a3[3] = 10;
        a2[4] = 4;
        a2[5] = 5;
        a2[6] = 6;
    
        a[0] = a2;
        a[1] = a3;
    
        std::cout << "a[0][5]: " << a[0][5] << std::endl;
        std::cout << "a[1][3]: " << a[1][3] << std::endl;
    
        /*****************************
            Showing various syntax
        *****************************/
    
        std::cout << (*a) << " = " << a[0] << " = " << &a2 << std::endl;
        std::cout << (a)[index] << " = " << (*(a+index)) << " = " << a3 << std::endl;
        std::cout << *(a)[index] << " = " << *(*(a+index)) << " = " << *a3 << std::endl;
        std::cout << *(a)[index]+2 << " = " << *(*(a+index)+2) << " = " << *(a3+2) << std::endl;
    
        return 0;
    }

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Your comments don't quite match the code. a is an array of pointers-to-int, not references-to-int. a2 is an array of ints, although when used by itself the name a2 can decay to a pointer-to-int (pointing specifically at the first member of the array).

  8. #8
    Registered User
    Join Date
    Dec 2013
    Posts
    9
    tabstop:In my mind a pointer is just like any other variable except it is designed to hold a reference. This is why I find my wording to be more clear. The way you're saying it is more official I think. An array most of the time acts just like a const pointer. So a2 also holds a reference in my mind.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    But pointers and references are two very distinct (but related) concepts in C++.

    You need to clarify your terminology, otherwise you're going to run into problems at some stage - like for example trying to describe a problem to some other coder.
    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.

  10. #10
    Registered User
    Join Date
    Mar 2012
    Posts
    110
    Ok, I see that my first post was missing information.
    Code:
      (*hist)  = (int *) malloc( ncolors * sizeof(int) );
    // populate histo and nc ...
      (*permut_index) = (int *) malloc( n * sizeof(int) );
      int pos;
      for(int j=0; j<n; ++j){
        pos=0;
    
        for(int k=0; k<nc[j]; ++k){
          pos += (*hist)[k];
        }
    
        pos+= colcount[nc[j]];
        (*permut_index)[pos]= j;
        ++colcount[nc[j]];
      }
    I guess this is a bit more informative.

  11. #11
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I have a sneaking suspicion that most of the arrays are declared wrong in the program. The extra layers of indirection aren't needed.

    Code:
    int *hist;
    int *permut_index;
    hist  = (int *) malloc( ncolors * sizeof(int) );
    // populate histo and nc ...
      permut_index = (int *) malloc( n * sizeof(int) );
      int pos;
      for(int j=0; j<n; ++j){
        pos=0;
     
        for(int k=0; k<nc[j]; ++k){
          pos += hist[k];
        }
     
        pos+= colcount[nc[j]];
        permut_index[pos]= j;
        ++colcount[nc[j]];
      }
    As people may have explained before int (*foo)[7] makes foo a pointer to an array of 7 ints. so the parentheses do matter here. (*foo) would dereference it, giving you the pointed to array object.

    Also you dereference in weird places. Don't call malloc() with (*pointer) or *pointer on the left side for now. The value of a pointer is a memory address, like how a whole number is the value of an int. When you dereference where you will store the address, you are indicating that the object being pointed to is another pointer. That is probably not the case.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    This sounds more like C and not C++. Are you sure it's not a C program/library?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Mar 2012
    Posts
    110
    Thanks to all, dazzling me with their knowledge... and to all eagerly pointing out things frustrating you in the code. I was just parallelizing some stuff and came across some lines I didn't get.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question on syntax
    By nasser in forum C Programming
    Replies: 5
    Last Post: 10-18-2011, 08:06 PM
  2. Syntax question
    By dotunix in forum C Programming
    Replies: 4
    Last Post: 12-31-2008, 05:59 AM
  3. Syntax question: &(++x)
    By yoshiznit123 in forum C Programming
    Replies: 8
    Last Post: 06-02-2006, 10:40 PM
  4. Syntax Question
    By Beaner in forum C++ Programming
    Replies: 3
    Last Post: 01-27-2006, 02:05 PM
  5. question on syntax
    By blue_gene in forum C++ Programming
    Replies: 2
    Last Post: 04-22-2004, 09:50 PM