i am noob. why wont this work?

This is a discussion on i am noob. why wont this work? within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> #include <cstdio> using namespace std; void disp(char *str[5]) { int choice=0; for(;;) { cout<<"Choose statement to read. ...

  1. #1
    Back after 2 years Panopticon's Avatar
    Join Date
    Dec 2002
    Posts
    262

    i am noob. why wont this work?

    Code:
    #include <iostream>
    #include <cstdio>
    using namespace std;
    void disp(char *str[5]) {
         int choice=0;
         for(;;) {
             cout<<"Choose statement to read. (1-5) Other to exit."<<endl;
             cin>>choice;
             if((choice<1)||(choice>5)) break;
             cout<<"Statement "<<choice<<" is \""<<str[choice-1]<<"\"."<<endl;
             }
         return;
         }
    int main() {
        char *str[5]={"","","","",""}, *input;
        cout<<"Please enter 5 strings to be stored."<<endl;
        for(int i=0;i<5;i++) {
                gets(str[i]); //I think the problem is here but i dunno what it is
                if(i<4) cout<<"Please enter next string."<<endl;
                }
        disp(str);
        cin.ignore();
        cin.ignore();
        return 0;
    }

    Thanks alot
    I AM WINNER!!!1!111oneoneomne

  2. #2
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    Hello,

    I believe because str is a string literal. After initialization, any alteration to your string is undefined. You can either allocate memory, or create a 2-dimensional array.

    As a string literal, I mean:
    Code:
    char *ptr = "Hello";
    You cannot alter that string. Though, the pointer can still be configured to point elsewhere, but modifying the strings contents after run-time is undefined.


    - Stack Overflow
    Last edited by Stack Overflow; 12-09-2004 at 04:47 PM. Reason: More info.
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  3. #3
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Quote Originally Posted by Panopticon
    i am noob. why wont this work?
    You just answered your own question.



    Besides, you should provide us a little more information than just a block of code if you want meaningful answers. Even though we're probably able to figure the code out given some time, it makes you look extremly lazy.
    Last edited by Sang-drax; 12-09-2004 at 04:48 PM.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  4. #4
    Back after 2 years Panopticon's Avatar
    Join Date
    Dec 2002
    Posts
    262
    ok mr. sang drax i admit im lazy

    the problem i think is in the line
    Code:
     gets(str[i])
    why cant i just make that pointer point to a string in the string table after the user inputs it?
    the whole program compiles fine but i get illegal op when i input the string

    i dont really understand overflow's explanation of literals
    shouldnt it work anyway since when the user inputs a string, the string itself has a memory address which can be pointed to

    thanks
    I AM WINNER!!!1!111oneoneomne

  5. #5
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    Let me break it down.

    Your two dimensional string works somewhat like this.

    A pointer is a variable that contains the address of a variable.

    Let's look closer:
    Code:
    char *str[5];
    You have a pointer. 5 strings that are currently "". You initialized them as string literals. You cannot change them unless you assign them to another variable that can be modified, or allocate a fresh block of memory to it.

    Simply, your pointer does not have an address that is yours to use. It is a temporary variable called a constant.


    - Stack Overflow
    Last edited by Stack Overflow; 12-09-2004 at 05:17 PM.
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  6. #6
    Back after 2 years Panopticon's Avatar
    Join Date
    Dec 2002
    Posts
    262
    But cant the memory address be changed depending on the user input?

    How would I achieve this then?
    Do i initialise the pointers to variables rather than cnstants then?
    thanks
    I AM WINNER!!!1!111oneoneomne

  7. #7
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    Hello,

    You could change str to "str[5][50]" if you don't want to worry about pointers. And also, you could use fgets() instead of gets(). This way you could use the appropriate function, and not worry about overflow.

    In this task, it would be useless to allocate memory to your string because you don't know how much you are getting.


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  8. #8
    Back after 2 years Panopticon's Avatar
    Join Date
    Dec 2002
    Posts
    262
    ill get this one day =/
    thanks
    I AM WINNER!!!1!111oneoneomne

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  2. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  3. Problems in getting OpenGL to work
    By zonf in forum C Programming
    Replies: 5
    Last Post: 02-13-2006, 03:48 AM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM

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