Thread: issue with point to array.

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    117

    issue with point to array.

    My function is this....
    Code:
    struct names *open_classes_taken(int *name_count, int *class_count[]);
    it returns a structure for me. And it uses an integer that i'll need later which is the first part. The second part it uses is an array of integers. Which I'll use later

    Code:
    int main(){
    
    
        int name_count = 0;
        int class_count[10] = {0};
        struct names *all;
     
        all = open_classes_taken(&name_count, &class_count[]);
    there is my main that is needed

    So my function is working. Getting an error of "syntax error before ] token" when I call my function. Trying to use it as a pointer. What am I doing wrong?
    I feel like this is a rookie question, but I was surfing the web and couldn't find much on it for what I'm trying to do.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by mgracecar
    it returns a structure for me.
    It returns a pointer to a struct names object.

    Quote Originally Posted by mgracecar
    So my function is working.
    You don't actually know that until you test, and you haven't tested because of the problem that you are facing

    Quote Originally Posted by mgracecar
    Getting an error of "syntax error before ] token" when I call my function. Trying to use it as a pointer. What am I doing wrong?
    Your open_classes_taken function's second parameter is a pointer to a pointer to int. You have an array of 10 ints. I suspect that you actually want your open_classes_taken function's second parameter to be a pointer to int:
    Code:
    struct names *open_classes_taken(int *name_count, int *class_count[]);
    Then you call it as:
    Code:
    all = open_classes_taken(&name_count, class_count);
    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

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    117
    Quote Originally Posted by laserlight View Post

    You don't actually know that until you test, and you haven't tested because of the problem that you are facing
    Well actually I tried the function without the parameter and it was working. I just added the parameter because I realized I need to use it to know how many spots there are in the structure. And that's where I got my problem

    and ok sorry on that. You're right. It returns a pointer to a structure.
    Anyways that part is working. I went ahead and checked the structure after it was returned and it's working perfectly

    And on the last part. What I'm wanting is to use this as a pointer to an array of ints. I'm going to need an int returned in that for each row of names.
    So now I think I see what you mean on the part....


    Code:
    all = open_classes_taken(&name_count, class_count);
    And that would be all I would need to change I would think? If so I'm getting an error of argument 2 is passed from incompatible pointer type.


    hmm. Any suggestions? I understand pointers fairly well until it gets into pointers to pointers. lol.

    Thanks for your help so far. Correct me if I'm misunderstanding you. And any help on my current error would be appreciated.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Oh dear, I forgot to modify the code snippet. Basically, I'm suggesting that your function be declared as:
    Code:
    struct names *open_classes_taken(int *name_count, int *class_count);
    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

  5. #5
    Registered User
    Join Date
    Feb 2012
    Posts
    117
    Works perfectly.
    Thanks a lot for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange issue in floating point addition
    By stephenwalter in forum C Programming
    Replies: 6
    Last Post: 02-23-2010, 02:28 AM
  2. floating point array
    By luigi40 in forum C# Programming
    Replies: 2
    Last Post: 12-09-2005, 04:37 PM
  3. Passing point to array[][] to a function
    By gazsux in forum C Programming
    Replies: 3
    Last Post: 04-29-2003, 07:45 AM
  4. point into an array
    By ekarapanos in forum C Programming
    Replies: 1
    Last Post: 04-17-2003, 09:10 AM
  5. How to point a pointer to an array
    By ripper079 in forum C Programming
    Replies: 8
    Last Post: 08-12-2002, 07:19 PM