storing dynamically allocated pointers

This is a discussion on storing dynamically allocated pointers within the C Programming forums, part of the General Programming Boards category; the well seems to getting deeper and deeper... > arrays cannot be assigned. so, this means the : Code: array[i] ...

  1. #16
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342
    the well seems to getting deeper and deeper...

    > arrays cannot be assigned.
    so, this means the :

    Code:
      
       array[i] = name ;
    is itself wrong ?
    but, we can assign one pointer to another and use the other pointer for referencing data for normal variables.. The array we have is a set of pointers in itself .. So, I got mislead into the notion that i can assign the "name " pointer into one of the array elements and then do what ever needed..

    yeah, ok I shud have done that long time back

    Code:
      void input ( char *names[]){
    	 int i;
    	char name[21];
    	
    	for(i=0; i<3; i++)
    	{
    		printf("enter the name \n");
    		fgets(name,20,stdin);
    
    		names[i] =  malloc (sizeof (char) * strlen(name) + 1);
    
    		names[i] = name ;
    		fputs(names[i],stdout);
    		printf("i=%d\n",i);
    		
    	}
    return ;
    }
    that is my function ..

    In main I just print them out :

    Code:
              for(count=0;count < 3; count++)
    	   fputs(names[count],stdout);
    I just hope ,i havent made any real blunder with my sleepy eyes here..
    Last edited by kris.c; 07-21-2006 at 10:30 PM.

  2. #17
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
      void input ( char *names[]){
    	 int i;
    	char name[21];
    	
    	for(i=0; i<3; i++)
    	{
    		printf("enter the name \n");
    		fgets(name,20,stdin);
    
    		names[i] =  malloc (sizeof (char) * strlen(name) + 1);
    
    		names[i] = name ;
    		fputs(names[i],stdout);
    		printf("i=%d\n",i);
    		printf("%u\n",names[i]);
    	}
    return ;
    }
    Do you see the memory leak in bold, and the difference between similar names in different colors? And do you understand what is going on?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #18
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342
    yeah, I think I do... better stick to the other method , i guess , instead of messing around with memory like this

  4. #19
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    If it's still not clear, it seems a bit like wondering what happens to the 42:
    Code:
    #include <stdio.h>
    
    int main (void)
    {
       int x;
       x = 42;
       printf("x = %d\n", x);
       x = -1;
       printf("x = %d\n", x);
       return 0;
    }
    
    /* my output
    x = 42
    x = -1
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #20
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342
    yeah, thanks dave..

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dynamically creating an array of pointers
    By cs32 in forum C Programming
    Replies: 4
    Last Post: 02-06-2008, 08:42 AM
  2. Dynamically allocated size
    By maverickbu in forum C++ Programming
    Replies: 12
    Last Post: 06-26-2007, 01:16 PM
  3. Dynamically Allocated Array
    By vb.bajpai in forum C Programming
    Replies: 3
    Last Post: 06-17-2007, 08:40 AM
  4. Replies: 5
    Last Post: 12-05-2002, 01:27 AM
  5. Replies: 4
    Last Post: 09-12-2001, 02:05 PM

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