typical error problem

This is a discussion on typical error problem within the C Programming forums, part of the General Programming Boards category; Hi guys, I really have been trying to fix this one myself but I am just learning C so I ...

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    29

    typical error problem

    Hi guys, I really have been trying to fix this one myself but I am just learning C so I my skill limits me, naturally.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    
    #define BUFFER 20
    
    int i;
    int iPort;
    void display_usage(void);
    char *target_ip,the_buffer[BUFFER],return_data[BUFFER];
    char input_Command[BUFFER];
    char jumble_data(char temp_data);
    
    int main( int argc, char *argv[] ) {
    
    	 if( argc < 2 )
             {
             display_usage();
             exit(1);
             }
            
    	 target_ip = argv[1]; // TARGETIP had to be a pointer
    	 iPort = atoi(argv[2]);
    
    	 // Prompt user for command
    	 printf("command>");
    
             if ( fgets(input_Command, sizeof input_Command, stdin) )
             {
             char *newline = strchr(input_Command, '\n');
                    if ( newline )
                    {
                    *newline = '\0'; /* strip trailing \n */
                    }
             }
    
    	 // send input to jumble_data
    	 *input_Command = jumble_data(input_Command);
          
          	 //setup sockets
             int testsocket;        
             struct sockaddr_in thetarget;  
             testsocket = socket(AF_INET, SOCK_STREAM, 0);  
    
             thetarget.sin_family = AF_INET; //always AF_INET        
             thetarget.sin_port = htons(iPort);         
             thetarget.sin_addr.s_addr = inet_addr(target_ip); // TARGETIP is argv[1]
    	
             connect(testsocket, (struct sockaddr *)&thetarget, sizeof(struct sockaddr)); // make the connection
    	 send(testsocket, input_Command, strlen(input_Command), 0);  // send the data
             
    	 /*while (1) {
    	 recv(testsocket, return_data, strlen(the_buffer), 0); 	
    	 printf("%s\n", return_data); 
    	 }*/
    
    	 perror("connect"); 
    	 return(0);
    }
    
    void display_usage(void)
    {
           fprintf(stderr, "Usage: ./connect 127.0.0.1 9999\n" );
    }
    
    /* Fucntion Summary:
    Take in user supplied command and inserts 4 random characters inbetween each character of the origianl command 
    */
    
    char jumble_data(char temp_Data) /* take in input_Command and put it in temp_Data */
    {
    	int i, k, j;
    	char jumbled_data[sizeof temp_Data * 4]; /* multiply temp_Data's array size by 4 to hold random chars */	
    
            for ( i = 0, k = 0; temp_Data[i] != '\0'; ++i ) /* until temp_data[i] reaches newline char, increment and loop */
            {
            jumbled_data[k++] = temp_Data[i]; /* assign temp_Data[i] to jumbled_Data[k] then increment (jumbled_Data[k++]) */
    
    	/* At this point if the user submitted command would have been "destroy", jumbled_Data[0] would contain the "d" and jumbled_D
    	ta[k] == jumbled_Data[1]. */
                    
    		for ( j = 0; j < 4; ++j ) /* until j == 4, increment */
                    {
                    jumbled_data[k++] = rand() % 26 + 'a'; /* fill next 4 char cells with a random character */ 
                    }
    		/* Now restart the loop */
            }
    return(jumbled_data[BUFFER]);
    }
    Please ignore my horrible style.

  2. #2
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    472
    Most people arnt even going to bother reading your code without you asking a question.

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    29
    Oh, yes, my fault. It was 3 am when I made this post I kind of shut down. Here are the errors I'm getting:
    Code:
    ob.c: In function `main':
    ob.c:41: warning: passing arg 1 of `jumble_data' makes integer from pointer without a cast
    ob.c: In function `jumble_data':
    ob.c:78: error: subscripted value is neither array nor pointer
    ob.c:80: error: subscripted value is neither array nor pointer
    Line 41 is:
    Code:
    *input_Command = jumble_data(input_Command);
    I think it may have something to do with my jumble_data function being broke.

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    input_Command is a char array.
    your prototype and definition for jumble_data is for a single char

    maybe change to
    Code:
    char jumble_data(char *temp_data);

  5. #5
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    Once you make it so that you're passing a char * to jumble_data() (you can write this as "char temp_data[]", btw), you'll also have to change the use of sizeof, as "sizeof(temp_data)" will return the size of a pointer to char, and "sizeof(*temp_data)" will return the size of a char, which is 1. If you're looking for the size of the array passed to jumble_data(), you'll have to do it differently. If you always pass input_Command to this function, you could simply say "sizeof(input_Command) * 4", as the compiler knows the size from your declaration. Alternatively, you may be able to get away with using a declaration like
    Code:
    char jumble_data(char temp_data[BUFFER])
    if the input array is always BUFFER chars, and then sizeof(temp_data) will return BUFFER * sizeof(char), as you expect.

    Your function jumble_data() returns a char. It is always the character at jumbled_data[20]. You are storing this single character at the beginning of the array input_Command. I doubt that this is what you intend.

    I suggest that you explore C arrays and pointers further, especially as they relate to strings in C.
    Insert obnoxious but pithy remark here

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    And:
    Code:
             if ( fgets(input_Command, sizeof input_Command, stdin) )
             {
             char *newline = strchr(input_Command, '\n');
                    if ( newline )
                    {
                    *newline = '\0'; /* strip trailing \n */
                    }
             }
    You should probably have an else in there to handle the case of fgets() failing.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. typical operations of an installer
    By stanlvw in forum Windows Programming
    Replies: 4
    Last Post: 05-30-2008, 08:07 AM
  3. typical access time of memory
    By Lind in forum Tech Board
    Replies: 5
    Last Post: 10-17-2007, 04:12 PM
  4. the typical * password thing
    By justforthis1 in forum C++ Programming
    Replies: 5
    Last Post: 10-26-2006, 01:48 PM
  5. Replies: 2
    Last Post: 10-08-2002, 09:31 AM

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