can't get it to work!!!

This is a discussion on can't get it to work!!! within the C Programming forums, part of the General Programming Boards category; Learning C......I need help What's wrong in the following codes....I want to keep the same format but I can't get ...

  1. #1
    Unregistered
    Guest

    Unhappy can't get it to work!!!

    Learning C......I need help

    What's wrong in the following codes....I want to keep the same format but I can't get it to work

    I am trying to type 8 lines of characters and then print them all
    I want to use malloc too....

    int main(void)
    {
    char (*p)[8];
    char line[8];
    int k;

    for(k=0;k<8;k++)
    {
    scanf("%s", &line);
    if (feof(stdin))break;

    p=(char (*)[8])malloc(sizeof line);
    if (p==NULL)
    {
    printf("storage: no storage available\n");
    return 1;
    }
    *p[8]=p; <======I thing this is wrong
    strcpy(p[8],line);
    }
    puts(p[8]);
    free(p);

    return 0;
    }

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Maybe this, then

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main(void) 
    { 
    char *p; 
    char line[8]; 
    int k; 
    
    for(k=0;k<8;k++) 
    { 
    	scanf("%s", line); 
    	if (feof(stdin))break; 
    		p = malloc(sizeof(line)); 
    	if (p==NULL) 
    	{ 
    		printf("storage: no storage available\n"); 
    		return 1; 
    	} 
    	strcpy(p,line); 
    } 
    puts(p); 
    free(p); 
    
    return 0; 
    }

  3. #3
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Oooops, forget that last post.

    [CODE]
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>

    int main(void)
    {
    char *p;
    char line[8];
    int k;

    for(k=0;k<8;k++)
    {
    scanf("%s", line);
    p = malloc(sizeof(line));
    if (p==NULL)
    {
    printf("storage: no storage available\n");
    return 1;
    }
    strcpy(p,line);
    }
    puts(p);
    free(p);

    return 0;
    }
    [CODE]

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    char (*p)[8];
    char line[8];
    Two things:

    1) Why are you using parenthesis there? If you want an array of pointers, just do:

    char *p[8];

    2) That's a mighty short line.

    Regarding printing them all, you have to use a loop to print them:

    for(x=0;x<8;x++)printf("%s\n",p[x]);


    Quzah.
    Last edited by quzah; 07-19-2002 at 10:17 PM.
    Hope is the first step on the road to disappointment.

  5. #5
    Unregistered
    Guest
    All previous suggestions produced errors in my code or did not print all 8 strings.

    This is what I have: tell me what is wrong with it?? Are the codes written correctly???


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

    int main(void)
    {
    char *p[8];
    char line[10];
    int k;

    for(k=0;k<8;k++)
    {
    scanf("%s", line);
    if (feof(stdin))break;

    p[8]=(char *)malloc(sizeof line);
    if (p==NULL)
    {
    printf("storage: no storage available\n");
    return 1;
    }
    strcpy(p[8],line);
    }
    for(k=0;k<8;k++)
    {
    printf("%s\n",p[8]);
    }
    free(p[8]);

    return 0;
    }

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    OK, keeping your format the same.... here's a fixed version.
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main(void)
    {
        char    *p[8];
        char    line[10];
        int     k;
    
        for (k = 0; k < 8; k++)
        {
            scanf("%s", line);
            if (feof(stdin)) break;
    
            p[k] = malloc(sizeof line);
            if (p == NULL)
            {
                printf("storage: no storage available\n");
                return(1);
            }
    
            strcpy(p[k], line);
        }
    
        for (k = 0; k < 8; k++)
        {
            printf("%s\n", p[k]);
            free(p[k]);
        }
    
        return(0);
    }
    I don't recommend using scanf() to get a "line" as it won't, it will get a word (using %s, it inputs up to the next white space character.

    Note that the free() is done with the for loop, this is because you malloc()'d 8 times within the previous loop. You cannot free all 8 allocations in one go.

    >malloc(sizeof line)
    There's no pointing in mallocing this size, the string might be shorter. Use this instead
    >malloc(strlen(line)+1)
    Also, malloc() requires stdlib.h

    >if (feof(stdin)) break;
    if you reach eof, because the user hits CTRL+d/z, you must not do the printf() loop 8 times, as the array won't have 8 valid pointers in it.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Unregistered
    Guest
    Thank you Hammer you are the best!!!!!!

    I just need a clarification what does the +1 do in
    malloc(strlen(line)+1)

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Unregistered
    I just need a clarification what does the +1 do in
    malloc(strlen(line)+1)
    The amount of memory needed to hold the string is:
    - The number of bytes within the string as returned by strlen()
    - Plus one more for the NULL terminator.

    If you forget the +1, you'll allocate 1 byte short, and when you do the strcpy(), the NULL will overwrite data that doesn't belong to that variable, maybe not even your program.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Unregistered
    Guest
    Thanks!!!!!!!!!!

  10. #10
    Registered User
    Join Date
    Jul 2002
    Posts
    4
    here is my suggestion for your program
    not sure if this is what you wish but i find ur code a bit too cryptic :P

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    	char line[25];	// change if u wish larger lines :)
    	char *p[8];
    	int index;
    
    	printf("type 8 lines of garbage");
    	for(index = 0; index < 8; index++) {
    		p[index] = malloc(sizeof line);
    		scanf("%s", p[index]);
    	}
    
    	printf("now printing the 8 lines");
    	for(index = 0; index < 8; index++) {
    		printf("%s\n", p[index]);
    	}
    
    	for(index = 0; index < 8; index++) {
    		free(p[index]);
    	}
    
    	return 0;
    }
    Last edited by iNFERNaL; 07-21-2002 at 02:57 AM.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    p[index] = malloc(sizeof(line[index]));
    You realize that you're only allocating one character here, right? sizeof( array[x] ) gives you the size of one array element. Not the size of the entire line.

    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    Jul 2002
    Posts
    4
    Originally posted by quzah


    You realize that you're only allocating one character here, right? sizeof( array[x] ) gives you the size of one array element. Not the size of the entire line.

    Quzah.
    hmm would changing that line to
    Code:
    p[index] = malloc(sizeof line);
    fix it? cause i tryed that way and didnt have any error..

  13. #13
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by iNFERNaL
    hmm would changing that line to
    Code:
    p[index] = malloc(sizeof line);
    fix it? cause i tryed that way and didnt have any error..
    Yes, it would. But I'd still recommend reading into a buffer, then malloc()'ing only the required amount of memory.

    Also, you must check the return from malloc. If it fails, it will return NULL, and in your code, the program will crash.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 08:48 PM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 11:50 AM
  3. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 11:39 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 05:36 PM

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