Arrays of Strings. With functions/memcpy?

This is a discussion on Arrays of Strings. With functions/memcpy? within the C Programming forums, part of the General Programming Boards category; I am super lost. You see when I'm at school I like to write functions about what I want to ...

  1. #1
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656

    Arrays of Strings. With functions/memcpy?

    I am super lost. You see when I'm at school I like to write functions about what I want to do when I get home. Well I was creating a sorting function that sorts an array of strings, it doesn't work just yet. Well, there's no C compiler here, and I was wondering if someone could help me out if I have concept problem with what's going on with this code:

    Code:
    #include <stdio.h>
    
    char **sortAll(const char *sortThis[255], const int nTotal) {
    	char *szSorted[255] = malloc(nTotal * sizeof(*szSorted));
    	strcpy(szSorted[0], sortThis[0]);
    	return szSorted;
    }
    
    int main(void) {
    	const char *szNotSorted[] = {
    		"abc",	"def"
    	}
    	char **szSorted;
    	memcopy(szSorted, sortAll(szNotSorted, 2));
    	puts(szSorted[0]); /* Hoping 'abc' :) */
    	return 0;
    }
    I've never used memcopy before lol, and it just looked right at the time. I highlighted the parts where I have no idea in red. Could you tell me what I'm doing wrong? Why it wouldn't compile correctly?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Your memory allocation is wrong. You can't simply malloc up a huge block and assign it to every member of an array in one shot. You have to loop through each element and assign them space individually. Furthermore, you shouldn't even bother with an array there. Just use a pointer to a pointer:
    Code:
    char **foo( const char *data[255], int nblocks )
    {
        char **bar;
        int x;
    
        /* error check nblocks here... */
    
        /* allocate nblocks worth of pointers */
        bar = malloc( sizeof( char * ) * nblocks );
        if( bar == NULL )
            return NULL; /* handle your error however, I'm doing this... */
    
        /* allocate space for each string... */
        for( x = 0; x < nblocks; x++ )
            bar[x] = malloc( sizeofyourstringspace );
    
        /* do stuff, copying or sorting your data */
    
    
        return bar;
    }
    You can actually "fake" sorting this. Instead of allocating space for each string and copying the data into it, simply use the pointers you've allocated and shuffle around what it points at, making them point to the first array's data in a sorted manner.

    Also, when you're done here, if in fact you are mallocing for each string, you need to free in the reverse manner in which you allocate:
    1) loop through and free each allocated string, one at a time.
    2) free the origional pointer chunk allocation
    Code:
    for( x = 0; x < nblock; x++ )
        free( bar[x] );
    free( bar );

    Quzah.
    Last edited by quzah; 01-10-2005 at 03:13 PM.
    Hope is the first step on the road to disappointment.

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Your spelling is also wrong. There is no 'o' in memcpy.

    Very brief reference: http://cppreference.com/stdstring_details.html#memcpy
    Last edited by Zach L.; 01-10-2005 at 04:37 PM.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  4. #4
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Quote Originally Posted by quzah
    Also, when you're done here, if in fact you are mallocing for each string, you need to free in the reverse manner in which you allocate:
    Code:
    for( x = 0; x < nblock; x++ )
        free( bar[x] );
    free( bar );
    But wouldn't that need to be:
    Code:
    for( x = nblock; x >= 0; --x )
        free( bar[x] );
    free( bar );
    ?

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No. The order you free each element in your block doesn't matter. It's just like an array of pointers which have data allocated for them. It doesn't matter how you move through the array freeing what's there, just so long as you cover every cell. You could skip around randomly if you felt like it.

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

  6. #6
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Code:
    bar = malloc( sizeof( char * ) * nblocks );
    if( bar == NULL )
         return NULL; /* Handle malloc() errors */
    How could bar == NULL if it's allocated memory? Could malloc mess up on mistake? I thought malloc was perfect :(
    Last edited by Kleid-0; 01-11-2005 at 06:54 PM.

  7. #7
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    Hello,

    According to this reference: If the system could not allocate the requested block of memory or if the size requested was 0 a NULL pointer is returned.


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  8. #8
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    I just checked my C reference, you're right. I love C, the more problems I have, the better this language gets! I am truley addicted.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  2. Replies: 2
    Last Post: 02-23-2004, 05:34 AM
  3. working with strings arrays and pointers
    By Nutka in forum C Programming
    Replies: 4
    Last Post: 10-30-2002, 07:32 PM
  4. strings or character arrays
    By Shadow12345 in forum C++ Programming
    Replies: 2
    Last Post: 07-21-2002, 10:55 AM
  5. Searching arrays for strings
    By Zaarin in forum C++ Programming
    Replies: 14
    Last Post: 09-03-2001, 06:13 PM

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