Find the answer to your Linux question:
Results 1 to 5 of 5
Hey, I'm pretty new to Linux/C programming and I have a problem with the following code: Code: #include <stdio.h> #include <string.h> #include <stdlib.h> int main (int argc, char **argv){ char ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Mar 2014
    Posts
    2

    Question Linux/C beginner problem


    Hey,
    I'm pretty new to Linux/C programming and I have a problem with the following code:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    int main (int argc, char **argv){
    	char set1[strlen(argv[1])];
    	char set2[strlen(argv[2])];
    	for (int y=0; y< strlen(argv[1]); y++){
    		set1[y] = *(argv[1]+y);
    		set2[y] = *(argv[2]+y);
    	}
    	printf("%s \n", set1);
    	printf("%s \n", set2);
    }
    (the arguments are 2 strings of the same length)
    well this still works fine. But after adding:

    Code:
    char text[23];
    while (fgets(text, 23, stdin) != 0){
    
    }
    anywhere set1 and set2 get random values. Any ideas how to fix that or what I'm doing wrong?

    (I'm not sure if this is the right area for this post, sorry if not)

  2. #2
    Just Joined! mrbruno's Avatar
    Join Date
    Jan 2013
    Location
    /MilkyWay/Sol/Earth/USA/NC/Raleigh
    Posts
    63
    I tried adding fgets() loop before and after your executable code and I didn't see any problems. Can you include a version of the program you use that does give you problems? What are the arguments you're using? What are you typing for the fgets() loop? Does it even get that far?

    I do see a problem which could result in unpredictable results. Remember that strings in C are typically followed by an extra null byte to indicate the end of the string. Functions like strlen() and strcpy() depend on this. You've neglected to account for the null terminator in your set1 and set2 buffers so it's luck if the printf()s were able to find the end of the strings. They could very well go off the end of the array, spewing off out trash that didn't come from argv[]. I recommend:

    1. When you allocate the arrays, make sure you add one more byte to the value that strlen() returns.
    2. Assuming you get arrays of the correct size, I'm not sure you can depend on the initial values of the bytes in the arrays. When you declare a variable this way, I think you have a pretty good chance of the values being initialized to null bytes but why risk it? I would recommend you remove all doubt and do it yourself with one of these methods:
      • Use memset() to set each array to null characters before you start transferring real data.
      • Set the single last byte by hand - either before or after you transfer real data
      • Use strcpy() to copy all the data plus the null terminator
      • Use strdup() to allocate a buffer with the proper size and copy all the data plus the null terminator - Since this does the equivalent of a malloc() on your behalf, get in the habit of free()ing such buffers. Using this method, you'll just want to allocate a pointer to receive the buffer's address.


  3. #3
    Linux Engineer
    Join Date
    Dec 2013
    Posts
    1,048
    Quote Originally Posted by mitch1993 View Post
    Hey,
    I'm pretty new to Linux/C programming and I have a problem with the following code:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    int main (int argc, char **argv){
    	char set1[strlen(argv[1])];
    	char set2[strlen(argv[2])];
    	for (int y=0; y< strlen(argv[1]); y++){
    		set1[y] = *(argv[1]+y);
    		set2[y] = *(argv[2]+y);
    	}
    	printf("%s \n", set1);
    	printf("%s \n", set2);
    }
    (the arguments are 2 strings of the same length)
    well this still works fine. But after adding:

    Code:
    char text[23];
    while (fgets(text, 23, stdin) != 0){
    
    }
    anywhere set1 and set2 get random values. Any ideas how to fix that or what I'm doing wrong?

    (I'm not sure if this is the right area for this post, sorry if not)
    In C a string always includes a null byte - /0 - you could call this a guard. I indicates to string functions that you are at the end of the string The strlen function doesn't count that byte, only the ones that precede it. When allocating space for a string you need to add one to the number of characters. After copying in a character loop you must append a '/0' to the end before using any functions that take a string - such as printf.

    If it worked as is it was an accident - accidents do happen.

    Code:
    char* mystring = "hello";
    int len = strlen (mystring);
    char copy[len + 1];
    int i =0;
    for (; i < len; ++i) {
        copy[i] = mystring[i];
    }
    copy[i] = '\0';

  4. #4
    Just Joined!
    Join Date
    Mar 2014
    Posts
    2
    Thank you for your answers. I'm going to try it when i get home.

  5. #5
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,411
    There is a problem here:
    Code:
    	char set1[strlen(argv[1])];
    	char set2[strlen(argv[2])];
    You need to add an additional byte for the terminating null character. IE,
    Code:
    	char set1[strlen(argv[1]) + 1];
    	char set2[strlen(argv[2]) + 1];
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •