#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    7
    Rep Power
    0

    Angry An Average programmer gone Novice trying to be Average again.


    I took C++ programming a very long time ago, but fell out of practice. I picked up PHP and blew it away, but now, bringing myself back to C++ is a route full of treachery and murderous heathens who want my scalp!

    Anyways, I'm a bit shakey overall, so some help would be most appreciative.

    I run...

    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>

    int main(int argc, char* argv[])
    {
    char* arg;

    scanf("%s", arg);
    printf("%s\n", arg);

    getch();
    return 0;
    }

    And I get...

    0xC0000005: Access Violation

    I hate my life.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,473
    Rep Power
    15
    I followed an eerily similar path, and now I'm re-learning C++, but I don't recognize any of your code. I looked up scanf in a book I have in my closet, and they put a dimension on the char*, which seems strange to me:

    Code:
    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    
    int main(int argc, char* argv[])
    {
    char* arg[20];
    
    scanf("%s", arg);
    printf("%s\n", arg);
    
    getch();
    return 0;
    }
    I would code that like this:
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    	string text;
    	cout<<"Enter some text:\n";
    	cin>>text;
    	cout<<"You entered: "<<text<<endl;
    	
    	return 0;
    }
    Last edited by 7stud; March 24th, 2003 at 11:24 PM.
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    7
    Rep Power
    0
    I guess I gotta shy away from those on-the-fly declared scalars in PHP, huh :-D

    from what I looked up, scanf enjoys strings, not chars. :(

    thanks
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,593
    Rep Power
    4207
    The error is because char *arg is not pointing to anything (i.e.) it is uninitialized. So when scanf reads the string and tries to assign it to arg, it ends up writing to wherever *arg is pointing to, which is causing it to crash. To fix this, make *arg point to something using malloc() or calloc(), like this:
    Code:
    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <malloc.h>
    
    int main(int argc, char* argv[])
    {
    char* arg;
    
    /* Allocate 100 character array */
    arg = (char *) malloc(100 * sizeof(char)); 
    
    scanf("%s", arg);
    printf("%s\n", arg);
    
    getch();
    free(arg); /* Don't forget to delete what we allocated */
    return 0;
    }
    Alternatively, you could declare arg as a char array instead of a char pointer and not use malloc(), like this:
    Code:
    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    
    int main(int argc, char* argv[])
    {
    char arg[100];
    
    scanf("%s", arg);
    printf("%s\n", arg);
    
    getch();
    return 0;
    }
    Either way should work well. Hope this helps :)
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,473
    Rep Power
    15
    Scorpions4ever,

    Then can I assume in C, this creates an array of pointers:

    char* arg[20];

    However, each pointer would seemingly be unitialized, so it still shouldn't work, but the code does work. :confused: (C doesn't make sense. Good thing Bjarne created some order out of that chaos.)
    Last edited by 7stud; March 25th, 2003 at 03:45 AM.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Posts
    272
    Rep Power
    18
    It doesn't work. It only appears to work. When arg is a single pointer to char its value is used as the parameter to scanf. Since it is uninitialized you get an access violation when it is dereferenced. When arg is an array of pointers to char its address is passed to scanf. Since this is a valid address scanf will stuff characters into memory at that address. In effect, you have created an array of 80 bytes and that is how scanf uses it. If you look at the memory in a debugger, you will see that the characters you scanned in are packed into the elements of arg.
  12. #7
  13. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    7
    Rep Power
    0
    I understand the pointer argument and it makes sense.

    As I try char arg[25], the code works fine.

    However, say I wish to use strlen or strcmp... both of them require a const char *. How would I go about using them in the event that I using the forementioned syntax array?

    Also, as I review a great dealof SMAUG mud code, all of the strings are char* string. It is very strange to me.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,473
    Rep Power
    15
    "How would I go about using them in the event that I using the forementioned syntax array?"

    In many situations, you can use an array name as if it were a pointer. To see that, print out a pointer variable--the result will be an address. If you print out an array name, the result will also be an address. The most significant difference between an array name and a pointer is that you can modify the address stored in a pointer, while the address that an array name refers to is fixed.

    "I wish to use strlen or strcmp... both of them require a const char *. "

    I don't know if you know this or not, but you don't have to send a constant pointer to a function that has a const pointer parameter. The way I look at is that a function which has a const designation in front of a parameter will take any parameter of the designated type const or non constant. What the const designation does is advertise that the function won't change the parameter. That allows the user/programmer to send a pointer to the function without worrying that the function will change the value pointed to by the pointer.

    You can run this code as a test:
    Code:
    #include <iostream>
    using namespace std;
    
    void print(const char* text)
    {
    	cout<<text<<endl;
    }
    int main()
    {
    
    	const char* texta ="Hello.";
    	char* textb = "Goodbye.";
    	print(texta);
    	print(textb);
    
    	return 0;
    }
    Last edited by 7stud; March 30th, 2003 at 06:34 PM.

IMN logo majestic logo threadwatch logo seochat tools logo