Thread: can someone give me a hand

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    39

    can someone give me a hand

    I dont get this'

    Im practiceing with some redirections. I wrote this and it works fine in my IDE using gnu

    Code:
    #include <stdio.h>
    #include<stdlib.h>
    
    
    
    
    
    int main(){
    
    int x;
    char *my_string[100]={'\0'};
    
    
    gets(my_string);
    
    
    puts("");
    
    
    printf("This came from test file %s \n\n\n\n\n",my_string);
    
    
    
    
    return;
    
    }
    I put the same thing in my text editor and run it using g++ and all i get back is these rediculous compile errors

    Code:
    warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char **’ [-Wformat]
    I dont get it
    Why will it let me use that little bit of code in my ide but not off the command line

    As i said i am trying ot get familiar with redirections. so im using the command line to read in a few lines of test i have stored in a .dat

    fyi im on ubuntu
    if that matters

    Can someone please help
    Is it just a warning and not an error?

  2. #2
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    Code:
    char *my_string[100]={'\0'};
    that means that each of the 100 elements is type char* (so 100 unallocated string). what you actually need is:

    Code:
     char my_string[100]
    Just curious, what IDE with what compiler did you use?
    "All that we see or seem
    Is but a dream within a dream." - Poe

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    That's a warning, not an error, and it's not ridiculous. %s expects a pointer to a string, which is to say, a char*. You're giving it an array of char*. Those are two different things.

    You should just do:
    Code:
    char my_string[100];
    And, of course, use fgets() instead of gets(). Plus if you're doing C, use gcc, not g++.

    I don't know what IDE you use, I don't know if it's hiding warnings, etc. But your code is incorrect, and g++ is right to complain.

  4. #4
    Registered User
    Join Date
    Jan 2012
    Posts
    39
    Quote Originally Posted by cas View Post
    That's a warning, not an error, and it's not ridiculous. %s expects a pointer to a string, which is to say, a char*. You're giving it an array of char*. Those are two different things.

    You should just do:
    Code:
    char my_string[100];
    And, of course, use fgets() instead of gets(). Plus if you're doing C, use gcc, not g++.

    I don't know what IDE you use, I don't know if it's hiding warnings, etc. But your code is incorrect, and g++ is right to complain.
    The IDE is CODEBLOCKS

    The compiler is both GCC sorry not G++

    And if im trying to use redirection why would i want to use fgets?

    Im redirecting the STD IN to come from a file

    So what you say about using fgets i do not understand

  5. #5
    Registered User
    Join Date
    Jan 2012
    Posts
    39
    Quote Originally Posted by nimitzhunter View Post
    Code:
    char *my_string[100]={'\0'};
    that means that each of the 100 elements is type char* (so 100 unallocated string). what you actually need is:

    Code:
     char my_string[100]
    My intent was to have a Char pointer pointing to array 100 wide each capable of handleing its own string
    and its element intialized to null


    Just curious, what IDE with what compiler did you use?
    My intent was to have a Char pointer pointing to array 100 wide each capable of handleing its own string
    and its element intialized to null

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Jan 2012
    Posts
    39
    Thanks for the link i know all that

    I dont get why this is so hard
    All i want is an array that has X amount of elements each that can store a string
    Thats it

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
    char words[5][30];
    int i;
    for(i =0;i<5;i++) {
        scanf("%s", words[i]);
        printf("%s\n", words[i]);
    }
    Remember with a string, it needs to hold that end of string char: '\0'. That means, in this case, that any word you enter over 29 letters, will put the program into either undefined behavior: ranging from a mild problem, to flat out crashing the program.

    Note that:
    Each "row" in the array, is another word, and the valid index numbers are 0 to 4, NOT 1 to 5.

    words[i] IS a pointer to the the first letter of that word, so no & is needed in the scanf().

    scanf() is used for user input for learners, BUT it is for strictly formatted data only, and is easily broken by a user's entry. The preferred method is to take in a string with fgets(), into a char buffer, and then to pick out what you want from there - but that's a too much for every little program a student needs to do - you wouldn't get a lot else done. Just be aware.
    Last edited by Adak; 04-22-2012 at 12:41 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please, can someone give a hand on this
    By code_lover in forum C Programming
    Replies: 5
    Last Post: 08-10-2011, 05:09 AM
  2. infix to postfix .... give me a hand please !!
    By backtolife in forum C++ Programming
    Replies: 6
    Last Post: 10-30-2006, 01:48 PM
  3. Replies: 1
    Last Post: 06-29-2004, 05:23 PM
  4. what are some other 'life skills' that can go hand in hand with programming
    By Shadow12345 in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 01-17-2003, 02:34 PM
  5. Hand Up
    By Rolf in forum C++ Programming
    Replies: 1
    Last Post: 12-02-2001, 08:44 AM