Thread: Pointers and Dynamic memory allocation help needed

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    5

    Pointers and Dynamic memory allocation help needed

    This code finds the longest common prefix in two strings.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    #define MAX_LENGTH 80
    
    
    int main ()
    {
      char   found_title[] = "The longest prefix is: ";
      char   not_found_title[] = "No common prefix has been found.";
      char   str1[MAX_LENGTH + 1];      /* The first string. */
      int    len1;                      /* Its length. */
      char   str2[MAX_LENGTH + 1];      /* The second string. */
      int    len2;                      /* Its length. */
      int    l_prefix = 0;              /* Length of the common prefix so far. */
      char   prefix[MAX_LENGTH + 1];
    
    
      /* Read the first input string and compute its length. */
      printf ("Please enter the first string: ");
      scanf ("%s", str1);
      len1 = strlen (str1);
    
    
      /* Read the second input string and compute its length. */
      printf ("Please enter the second string: ");
      scanf ("%s", str2);
      len2 = strlen (str2);
    
    
      /* Find the longest common prefix. */
      while (l_prefix + 1 <= len1 && l_prefix + 1 <= len2 &&
             strncmp (str1, str2, l_prefix + 1) == 0)
      {
        l_prefix++;
      }
    
    
      /* Print the results. */
      if (l_prefix == 0)
      {
        printf ("%s\n", not_found_title);
      }
      else
      {
        /* Copy the prefix and print it. */
        strncpy (prefix, str1, l_prefix);
        prefix[l_prefix] = '\0';
        printf ("%s%s\n", found_title, prefix);
      }
    
    
      return (0);
    }
    Now I need to edit it so instead of printing out the longest common prefix of the two strings which it receives as two array arguments, it returns a pointer to a new array in which the answer is stored.

    I know I should use dynamic memory allocation but I'm not sure if I should use calloc or malloc?

    Thanks

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    It doesn't matter. Which ever one you use, make sure you allocate enough space for the prefix, plus one extra byte for the null. calloc will zero out the bytes for you, malloc will not (there will be garbage in there). Either way, you'll overwrite it with the prefix. And of course, free() it when you're done.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    5
    Thanks, can you point me in the right direction to edit the code?

    Is this the right idea:
    Code:
    	//allocate unused space for the array
    	pointerData = (char**) calloc (l_prefix, sizeof(char));

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    You only need pointerData to be a char *, not a char **. Also, don't cast malloc (read this link)
    Code:
    char *pointerData;
    pointerData = calloc(l_prefix, sizeof(char));
    strncpy(pointerData, ...);  // just like how you copy into prefix in your current code
    // do stuff with pointerData
    free(pointerData);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Memory Allocation
    By slash.hack in forum C Programming
    Replies: 9
    Last Post: 09-30-2011, 04:31 AM
  2. Link List,Structures,Dynamic Memory Allocation,Pointers
    By Tanuj_Tanmay in forum C Programming
    Replies: 2
    Last Post: 04-24-2009, 08:55 PM
  3. Dynamic Memory Allocation
    By oranges in forum C++ Programming
    Replies: 3
    Last Post: 11-28-2006, 07:50 AM
  4. Dynamic memory allocation...
    By dicorr in forum C Programming
    Replies: 1
    Last Post: 06-24-2006, 03:59 AM
  5. dynamic memory allocation and returning pointers
    By sballew in forum C Programming
    Replies: 7
    Last Post: 11-03-2001, 03:21 PM