Thread: Pointer help

  1. #1
    Registered User
    Join Date
    Nov 2011
    Location
    Bucureşti, Romania
    Posts
    3

    Pointer help

    Hello, I am new here but i have a question regarding pointers.
    I am using Dev-C++ and i have the following question:

    I have a function

    Code:
    simbol* alfabet( FILE* fisier)
    { ...
    
    simbol* v;
    ...
    
    return v; 
     
    }
    So in main I want to call this function. It returns the start address of the pointer v but I can't get the dimension of the pointer.

    Any idea how can I get the dimension of the pointer that I filled in the function?

    Thank you.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What do you mean by "dimension of the pointer"?
    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
    Nov 2011
    Location
    Bucureşti, Romania
    Posts
    3
    I mean the number of elements, of the type simbol,I put in the pointer.

    For example:
    In the function alfabet I add 10 element to v, all with the type simbol. In this i know the exact number of elements.

    In main i call my function and it returns the start of the pointer.
    How can i find out the number of element i added to the pointer.

    I tried to make the dimension as a global variable but i want to know if there is another way, except this one.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    A pointer is just a pointer, not a container.

    There are a few ways to deal with this, but one way is to #include <vector> then just use and return (or modify through a reference parameter) a std::vector<simbol>.
    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
    Dec 2007
    Posts
    2,675
    Pass in a variable by reference to receive the number of elements you read?

    Code:
    simbol* alfabet( FILE* fisier, unsigned int &numElements)
    {
        // Set numElements in here
    }
    
    int main()
    {
        unsigned int numElements = 0;
        simbol *s = alfabet(filePtr, numElements);
    }
    Hope you're dynamically allocating the memory for that pointer in the function, or you will lose whatever it's pointing to when you leave the function.

    Also, there's no indication that this is actually a C++ program, as all you show is the use of FILE, which is C I/O. If you're actually writing C, then you need to pass a pointer to the argument as C does not support pass-by-reference:

    Code:
    simbol* alfabet( FILE* fisier, unsigned int *numElements)
    {
        *numElements = numRead;
    }
    
    int main()
    {
        unsigned int numElements = 0;
        simbol *s = alfabet(filePtr, &numElements);
    }

  6. #6
    Registered User
    Join Date
    Nov 2011
    Location
    Bucureşti, Romania
    Posts
    3
    Thank you very much rags_to_riches. The first solution worked.
    I am dynamically allocating the memory for the pointer in the function, and after I added the part you wrote it works perfectly.

    For the thing about what language I use, I am also very confuse. My teacher told me that this is C++ but i find different things on the internet and I don't know what to belive anymore.

    Thank you very much again because this is a very important project.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    This code will indeed compile with a C++ compiler, hence it is valid C++ code.
    However, this code will also compile with a C compiler, hence it is also valid C code.
    This implies that this can be either C or C++ code.

    So the conclusion we can make is:
    - either the teacher is really teaching C that can or is compiled with a C++ compiler,
    - or the teacher is teaching the C subset of C++ and is (possibly) intending to move over to C++ constructs in the future.

    Hope that answers your question.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 01-28-2010, 02:44 AM
  2. Replies: 3
    Last Post: 10-30-2009, 04:41 PM
  3. Replies: 9
    Last Post: 06-13-2009, 02:31 AM
  4. Replies: 1
    Last Post: 03-24-2008, 10:16 AM
  5. Replies: 4
    Last Post: 08-27-2007, 11:51 PM