Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,605 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 904 people online right now. Registration is fast and FREE... Join Now!




Pointer and Strings

3 Pages V  1 2 3 >  
Reply to this topicStart new topic

Pointer and Strings

Rating  5
Louisda16th
post 10 Aug, 2006 - 01:13 AM
Post #1


 

Group Icon
Joined: 3 Aug, 2006
Posts: 1,790



Thanked 1 times

Dream Kudos: 755
My Contributions


Im presently learning pointers to strings (From "Lets Us C" by Yashwant Kanetkar). While explaining arrays of pointers to strings, the book says that we cannot recieve strings through pointers as shown below:
CODE

main()
{
      char *names[6];
      int i;
      for(i=0;i<=5;i++)
      {
          printf("\nEnter name");
          scanf("%s",name[i]);
       }
}


Its says that u can get out of this problem like this
CODE


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

void main()
{
    char *names[6];
    char n[50];
    int len, i;
    char *p;

    for(i=0;i<=5;i++)
    {
        printf("\nEnter Name");
        scanf("%s",n);
        len=strlen(n);
        p=malloc(len+1);
        strcpy(p,n);
        names[i]=p;
    }
    for(i=0;i<=5;i++)
        printf("\n%s",names[i]);
}



now this program gives da following error:
cannot convert from 'void *' to 'char *' Conversion from 'void*' to pointer to non-'void' requires an explicit cast.
wat does this mean an wat do i do about it.
also i havent understood the use of malloc() here. n wats da need of saying:
CODE

p=malloc(len+1);
strcpy(p,n);
names[i]=p;

sad.gif
User is offlineProfile CardPM

Go to the top of the page

Mrafcho001
post 10 Aug, 2006 - 02:08 AM
Post #2


D.I.C Addict

Group Icon
Joined: 1 Nov, 2005
Posts: 753



Thanked 5 times

Dream Kudos: 120
My Contributions


for a task as simple as this you dont need to use pointers...char name[6];

but if you must.. in C++ you use the new and delete keywords.

CODE

char *name = new char[6];
//... use it.
//when no longer needed
delete [] name;
User is offlineProfile CardPM

Go to the top of the page

Louisda16th
post 10 Aug, 2006 - 03:50 AM
Post #3


 

Group Icon
Joined: 3 Aug, 2006
Posts: 1,790



Thanked 1 times

Dream Kudos: 755
My Contributions


QUOTE(Mrafcho001 @ 10 Aug, 2006 - 03:38 PM) *

for a task as simple as this you dont need to use pointers...char name[6];

but if you must.. in C++ you use the new and delete keywords.

CODE

char *name = new char[6];
//... use it.
//when no longer needed
delete [] name;


Thanx. But can u xplain wat malloc() is n wat it does?

This post has been edited by Louisda16th: 10 Aug, 2006 - 04:26 AM
User is offlineProfile CardPM

Go to the top of the page

Mrafcho001
post 10 Aug, 2006 - 04:14 AM
Post #4


D.I.C Addict

Group Icon
Joined: 1 Nov, 2005
Posts: 753



Thanked 5 times

Dream Kudos: 120
My Contributions


malloc() allocates memory. When you create a pointer (without initializing it) it points to a random memory address which is probably being used by something else. malloc reserves a number of bytes and points the pointer to them. So the pointer doesnt override other data which could be in use by another program.

free() must be used to free the memory allocated or itll cause memory leak...

again use new and delete instead. malloc and free are in use only in C
User is offlineProfile CardPM

Go to the top of the page

Louisda16th
post 10 Aug, 2006 - 04:17 AM
Post #5


 

Group Icon
Joined: 3 Aug, 2006
Posts: 1,790



Thanked 1 times

Dream Kudos: 755
My Contributions


QUOTE(Mrafcho001 @ 10 Aug, 2006 - 05:44 PM) *

malloc() allocates memory. When you create a pointer (without initializing it) it points to a random memory address which is probably being used by something else. malloc reserves a number of bytes and points the pointer to them. So the pointer doesnt override other data which could be in use by another program.

free() must be used to free the memory allocated or itll cause memory leak...

again use new and delete instead. malloc and free are in use only in C


Thanx
User is offlineProfile CardPM

Go to the top of the page

born2c0de
post 10 Aug, 2006 - 04:21 AM
Post #6


printf("I'm a %XR",195936478);

Group Icon
Joined: 26 Nov, 2004
Posts: 3,905



Thanked 34 times

Dream Kudos: 2800

Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions


Thanks for the prompt reply Mrafcho001!!
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 10 Aug, 2006 - 04:27 AM
Post #7


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,176



Thanked 33 times

Dream Kudos: 25
My Contributions


Just to clarify, C is the language being used in this program, not C++, so the new and delete operators are not available.
User is offlineProfile CardPM

Go to the top of the page

Louisda16th
post 10 Aug, 2006 - 04:29 AM
Post #8


 

Group Icon
Joined: 3 Aug, 2006
Posts: 1,790



Thanked 1 times

Dream Kudos: 755
My Contributions


then wat can u do in c?
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 10 Aug, 2006 - 04:36 AM
Post #9


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,176



Thanked 33 times

Dream Kudos: 25
My Contributions


Use malloc() and free().
User is offlineProfile CardPM

Go to the top of the page

born2c0de
post 10 Aug, 2006 - 07:58 AM
Post #10


printf("I'm a %XR",195936478);

Group Icon
Joined: 26 Nov, 2004
Posts: 3,905



Thanked 34 times

Dream Kudos: 2800

Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions


QUOTE
Use malloc() and free().

...or even calloc() depending on nature of variable and need.
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 10 Aug, 2006 - 09:24 AM
Post #11


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,176



Thanked 33 times

Dream Kudos: 25
My Contributions


Very good point...in fact calloc() would be the preferred choice here as it is an array that is being declared.
User is offlineProfile CardPM

Go to the top of the page

Louisda16th
post 10 Aug, 2006 - 07:33 PM
Post #12


 

Group Icon
Joined: 3 Aug, 2006
Posts: 1,790



Thanked 1 times

Dream Kudos: 755
My Contributions


Thanx guys. The code works but i had 2 add smthing:
CODE

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

void main()
{
    char *names[6];
    char n[50];
    int len, i;
    char *p;

    for(i=0;i<=5;i++)
    {
        printf("\nEnter Name");
        scanf("%s",n);
        len=strlen(n);
        p=(int*)malloc(len+1);  /*da prob was here*/
        strcpy(p,n);
        names[i]=p;
    }
    for(i=0;i<=5;i++)
        printf("\n%s",names[i]);
    free(p);
}

I jus added da (int*) b4 da malloc function cause i saw it i sm other place (a book we used in skool, Programming in C by CBM publications it said its called type casting. smthing im yet 2 learn.) smile.gif

This post has been edited by Louisda16th: 11 Sep, 2006 - 09:17 PM
User is offlineProfile CardPM

Go to the top of the page

3 Pages V  1 2 3 >
Reply to this topicStart new topic
Time is now: 11/23/08 02:28AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month