String Memory Allocation Program Help

This is a discussion on String Memory Allocation Program Help within the C Programming forums, part of the General Programming Boards category; So this program is supposed to take the input string and break it down to each individual argument into separate ...

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

    String Memory Allocation Program Help

    So this program is supposed to take the input string and break it down to each individual argument into separate lines then add ".txt" to each end of the arguments after.
    For example if I prompt
    ./nondebugmain programming is rewarding
    The program should do this:
    ./nondebugmain
    programming
    is
    rewarding
    ./nondebugmain.txt
    programming.txt
    is.txt
    rewarding.txt

    Here's my code so far.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void main(int argc, char **argv)
    {
    
    int i;
    int size;
    char *new=NULL;
    
    // print out each command-line argument on a separate line.
    for(i=0;i<argc;i++)
    printf("Command-line argument %d: %s\n",i,argv[i]);
    
    for(i=0;i<argc;i++)
    new = (char*)malloc(strlen(argv[i]+5));
    
    for(i=0;i<argc;i++)
    {    
    strcpy(new,argv[i]);
    strcat(new,".txt");
    argv[i]=*new;
    }
    
    // print out the arguments again
    for(i=0;i<argc;i++)
    printf("Command-line argument %d: %s\n",i,argv[i]);
    
    }
    There seems to be an error I missed and I cannot seem to be able to find it. Any tips would be nice and helpful. Thank you in advance

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by leinad0213 View Post
    Code:
    new = (char*)malloc(strlen(argv[i]+5));
    This has the unfortunate effect of doing the exact opposite of (what I assume) is the intention. You get the length of the string in argv[i] -5.

    The reason is the argv[i] is a pointer, pointing at the first character of the string. If you add 5 to that address you will move ahead 5 characters in the string, and consequently 5 characters closer to the end.

    What you need to do is to take the length of the string and add 5 to the argument of malloc.

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    7
    Oh just noticed that bug. I fixed it but I keep getting the error "main.c:25:10: warning: assignment makes pointer from integer without a cast [enabled by default]"

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    It's this line here, "new" is a pointer when you add * in front of it here you are dereferencing it. The effect is that you are assigning a char to a pointer. Just get rid of the *.

    Code:
    argv[i]=*new;
    I'm not sure about re-assigning stuff to argv.

    You should also use int main btw.

    Edit:

    Wait. This wont work:

    Quote Originally Posted by leinad0213 View Post
    Code:
    for(i=0;i<argc;i++)
    new = (char*)malloc(strlen(argv[i]+5));
    I didn't notice the for loop. You are overwriting new in each iteration of the loop, unless argc is 1 that is.
    Last edited by Subsonics; 01-12-2012 at 02:00 AM.

  5. #5
    Registered User
    Join Date
    Jan 2012
    Posts
    7
    You sir were an amazing help. Removing the * term from argv[i] = *new worked like a charm and the program ran just as intended. I lift my hat to you. Kudos and thank you so very much

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bug in Best-Fit Memory Allocation program (Simulation)
    By RommelTJ in forum C Programming
    Replies: 6
    Last Post: 12-13-2009, 03:43 PM
  2. Memory allocation overflow in c program
    By bassam_fci in forum C Programming
    Replies: 6
    Last Post: 05-19-2009, 03:35 PM
  3. Memory Allocation Using String (Please Help)
    By kolliash in forum C Programming
    Replies: 5
    Last Post: 05-06-2008, 03:32 AM
  4. Memory Allocation In A String Class
    By mike_g in forum C++ Programming
    Replies: 13
    Last Post: 09-18-2007, 10:34 AM
  5. Memory allocation
    By Leiken in forum C++ Programming
    Replies: 5
    Last Post: 02-22-2002, 03:32 PM

Tags for this Thread


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