Thread: I just don't get it!

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    24

    Unhappy I just don't get it!

    Can someone tell me what's wrong with the following?
    Do you think that my assignment 'makes integer from pointer without a cast' on line 22? That's what gcc seems to be thinking

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

    char* linker( void );
    char* s1 = "Helloooo,";
    char* s2 = "World!";
    char* Some;

    int main ( void )
    {

    int len;
    int len2;

    len = strlen(s1);
    len2 = strlen(s2);

    /* The Line below is 22 */
    *Some = linker();
    /* it used to be *Some = linker( len, len2 );
    then I changed it hoping it would eliminate
    the problem but it didn't */

    printf("\n %c \n" , *Some);

    return 0;
    }

    char* linker ( void )
    {
    char* s3;

    s3 = strcat ( s1 , s2 );

    return s3;
    }

    The linker function used to be char * linker (int a , int b) to recieve the len and len2 values but I kept getting the same message! then I changed it to what it is now

    I then tried Some = &linker();
    *Some = linker();
    Some = linker();
    and *Some = &linker();

    but none of them work. in the cases with '&', I don't get the makes integer from pointer message, but I get the following.

    D9Ex7.c: In function `main':
    D9Ex7.c:22: invalid lvalue in unary `&'

    Any help?
    Thanx in advance for your time

    PS: I didn't know how to insert 'CODE' dialogue. sorry for the messy code!
    Last edited by Rhodium; 01-17-2003 at 08:59 PM.
    Thanx
    Rhodium

  2. #2
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    You have quite a lot of mistakes in that program -- not just syntactically, but logically.

    strcat concatenates the 2nd string onto the end of the first string (in other words, it actually stores it to the first string). You have to make sure you allocated enough memory for the string (which you didn't).

    Also, you are trying to edit a string (via concatenation) that is in unwritable memory AND wouldn't have enough space to handle the concatentation.

    Then, you are trying to store an address to a char. You shouldn't be dereferencing the pointer.

    You are using printf improperly if you want the string to be displayed.

    You include stdlib but never use anything declared in it.

    You also made your variables uneccisarily global (make them local and pass their addresses to the linker function) and you made length variables which you initialized yet never used.

    When it comes down to it, all you really are doing is encapsulating the strcat function with no changes. You might as well just directly using the strcat function.

    Here's the fixed program none-the-less:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    char* linker( char*, const char* );
    
    int main ( void )
    {
    
        char  s1[17] = "Helloooo, ",
            * s2 = "World!";
    
        printf( "%s\n", linker( s1, s2 ) );
    
        return 0;
    }
    
    char* linker( char* str1, const char* str2 )
    {
        return strcat ( str1 , str2 );
    }

  3. #3
    Registered User
    Join Date
    Dec 2002
    Posts
    24
    Thanx a lot Polymorphic OOP,

    As I said, I changed a lot of things trying to fix numerous problems - the problems were a page long at the begining! - so for example I used to use malloc() but after erasing it, I forgot to erase stdlib.h inclusion. Anyways, thanx for your FAST reply.

    What's the difference between declaring variables global and local?
    And what if I want to allocate enough space for the concatenated string using malloc()? instead of declaring s1[17]? And I want to put the concatenated strings in a third string?

    Thanx a lot Poly,
    Thanx
    Rhodium

  4. #4
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by Rhodium
    Thanx a lot Polymorphic OOP,

    As I said, I changed a lot of things trying to fix numerous problems - the problems were a page long at the begining! - so for example I used to use malloc() but after erasing it, I forgot to erase stdlib.h inclusion. Anyways, thanx for your FAST reply.

    What's the difference between declaring variables global and local?
    And what if I want to allocate enough space for the concatenated string using malloc()? instead of declaring s1[17]? And I want to put the concatenated strings in a third string?

    Thanx a lot Poly,
    np

    You want to use local variable in most cases because it limits their scope to that function (globals can be accessed from anywhere which isn't a good idea). Also, it keeps the variables in memory only while the function is being called. Plus, it makes your functions more modular -- now all you have to do is change what you pass to the function to get a different result. If you used global variables you would have to have completely rewritten the function everytime you wanted it to modify another string, or you'd have to always put your data into those global variables.

    If you use malloc, just make sure that you allocate at least the length of both the strings plus one bytes. Then, copy all of the data in from the base string into the newly allocated array, then use strcat on that newly allocated array to concatenate the second string.

    Voila!

    Just make sure that you remember to free the memory you allocated.

  5. #5
    Registered User
    Join Date
    Dec 2002
    Posts
    24
    This is the code I wrote trying to use malloc ( and I didn't free the allocated space 'cause I didn't know how to ). The code is error free to the compiler but the output is still not what I expected.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    char* linker( char* , const char*, int len, int len2 );
    
    int main ( void )
    {
            char* s1 = "Hellooo,";
            char* s2 = "World!";
            int len;
            int len2;
    
            len = strlen(s1);
            len2 = strlen(s2);
    
            printf("\n %s \n", linker(s1, s2, len, len2));
    
            return 0;
    }
    
    char* linker ( char* str1, const char* str2, int len, int len2 )
    {
            str1 = malloc( len + len2 + 1 * sizeof(char));
            return strcat ( str1, str2 );
    }
    the output is:


    World!
    $
    [note : the $ sign only represents the Linux's bash shell which apears at the end of the execution and it's not a part of the output.]

    Any help on this?
    I thought it might have something to do with calling linker() in a printf function and sending int values to linker() while printf is looking for strings. But the returning value of linker is still a string!

    Thanx for any help
    Thanx
    Rhodium

  6. #6
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,227
    Correct me if I'm wrong, but this:

    str1 = malloc( len + len2 + 1 * sizeof(char));

    effectively wipes out whatever value you had in str1. If you really need to re-allocate memory, there's a realloc() function...

  7. #7
    Registered User
    Join Date
    Dec 2002
    Posts
    24
    YES ................. IT FINALLY WORKED THANK YOU ALL FOR HELPING >>> SPECIALLY POLYMORPHIC OOP>>> RHODIUM

    Thanks to All, it took me too weeks to get it working!
    Thanx
    Rhodium

Popular pages Recent additions subscribe to a feed