Thread: Need help with a small problem

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    19

    Question Need help with a small problem

    Given the following program :

    Code:
    #include <iostream>
    using namespace std;
    
    struct quest
    {
      int * ptr;
      char c;
    };
    
    int main()
    {
         quest q;
         *q.ptr = 7;
         q.c = '4';
         return 0;
    }
    I am supposed to fix the error in the program without adding or changing any variable names or data types. The error I get when I run this program is "c4700: uninitialized local variable 'q' used." I think that this is because in the statement "*q.ptr = 7;" 7 is being assigned to the variable pointed to by ptr, but ptr has not been set to point to another variable yet. I thought of declaring another variable and having ptr point to it, but my instructions say to not add any other variables. How do I fix this without changing or adding any variables?

  2. #2
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    Quote Originally Posted by Freddy92 View Post
    Given the following program :

    Code:
    #include <iostream>
    using namespace std;
    
    struct quest
    {
      int * ptr;
      char c;
    };
    
    int main()
    {
         quest q;
         *q.ptr = 7;
         q.c = '4';
         return 0;
    }
    I am supposed to fix the error in the program without adding or changing any variable names or data types. The error I get when I run this program is "c4700: uninitialized local variable 'q' used." I think that this is because in the statement "*q.ptr = 7;" 7 is being assigned to the variable pointed to by ptr, but ptr has not been set to point to another variable yet. I thought of declaring another variable and having ptr point to it, but my instructions say to not add any other variables. How do I fix this without changing or adding any variables?
    You can use "new" to allocate memory for ptr. then you can store a value at that memory location.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    19
    Thank you very much.

  4. #4
    Registered User
    Join Date
    Dec 2010
    Posts
    19
    Sorry for the double post, I just have another question and didn't see an edit button.

    I was trying to figure out when it would be legal to use the ++ operator on an array. For instance, if you have:
    Code:
     int arr = {1, 2, 3, 4, 5};
    could the statement arr++ be used to increment the address or something? I'm just trying to figure out exactly what ++ can be used for. Thanks.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You can't increment arrays. However, you can increment pointers so they progressively act on elements of an array.

    For example;
    Code:
    #include <iostream>
    int main()
    {
          int arr[] = {1, 2, 3, 4, 5};
    
          int *p = arr;
          int *end = arr + 5;    // as we have 5 elements in array
    
          while (p != end)
          {
               std::cout << *p << '\n';
               ++p;
          }
    }
    will print the elements of your array, one per line.

    Note that end points one past the last element of the array. That means it cannot be dereferenced, but it does provide a marker for ending the loop.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Don't forget that when using new, you must also use delete.
    As for useful reading, I'd recommend smart pointers. They always go hand-to-hand with new, so it is useful reading and knowledge.
    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. Small Problem with double and integer adding?
    By Nathan the noob in forum C++ Programming
    Replies: 5
    Last Post: 03-28-2009, 04:16 PM
  2. Visual C++ small problem
    By gadu in forum C++ Programming
    Replies: 0
    Last Post: 03-10-2009, 10:45 PM
  3. Small problem with this array...
    By Merholtz in forum C Programming
    Replies: 7
    Last Post: 11-03-2008, 04:16 PM
  4. Help with a small problem (beginner)
    By piffo in forum C Programming
    Replies: 13
    Last Post: 09-29-2008, 04:37 PM
  5. Need Big Solution For Small Problem
    By GrNxxDaY in forum C++ Programming
    Replies: 8
    Last Post: 08-01-2002, 03:23 AM