Pointer help

This is a discussion on Pointer help within the C++ Programming forums, part of the General Programming Boards category; Hello, I am new here but i have a question regarding pointers. I am using Dev-C++ and i have the ...

  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
    22,308
    What do you mean by "dimension of the pointer"?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    22,308
    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>.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,657
    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
    Posts
    23,031
    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, 05:41 PM
  3. Replies: 9
    Last Post: 06-13-2009, 03:31 AM
  4. Replies: 1
    Last Post: 03-24-2008, 11:16 AM
  5. Replies: 4
    Last Post: 08-28-2007, 12:51 AM

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