Introduce me to malloc ()

int *pointer;

pointer=malloc(2) ;

*pointer=index;


allocate storage space for an integer variable. PLease tell me if this is true.


does the for loop go like this?

Do yo have to declare it several times or only once?


for (index=8; (index < 10); index++

printf("%d, index) ;

for.... should I stop this for loop? does a for loop only go once till invisibly come to false to 10?

Do I have to declare as many times as 10 goes up to?


Should I use break to break out of a for loop earlier?


Just answer that question, should I only write a for loop ones or 10 times? from 8?



How does strlen give you the size of a string? from printf? do you have to print it out? studdy:


void main()

}


int index;

int *ptr;

ptr=malloc(2) ;

*ptr=index;

printf("%d
", index) ;

index=5;

for (index=5; (index < 7); index++

printf("%d, index) ;

break;


printf("%d, index) ;

puts("Its now incremented to 6") ;

_________________________________


char *string1[20]="Hellow";


does the * before the string tell it to point to first element of the string, [0]? which is 'H' ?


thanks.


Comments

  • malloc (and its partner free) allow you to grab memory and give it back 'dynamically' so memory grabbed in this way is usually referred to as 'dynamic memory' and classified as 'dynamically allocated'.




    : int *pointer;

    : pointer=malloc(2) ;

    : *pointer=index;


    there's a couple of different reasons why this won't work. First, you're assuming an integer is two bytes big. On some compilers, it's four bytes big, so you'd be allocating too little space. When using malloc, it is almost always the case that you will use the sizeof operator to determine the correct size to allocate (see below).


    Second, malloc returns a 'pointer to void' which is incompatible with 'pointer to int'. C is a typed language, so it will not automatically accept this as what you want. You must 'typecast' the pointer:


    pointer=(int *)malloc(sizeof(int));




    : allocate storage space for an integer variable. PLease tell me if this is true.


    It is true. However, unless you really need to dynamically allocate it like this, there's not much point. Just declare an integer variable. It's so much more work to dynamically allocate and free one. However dynamically allocated space is the only way to have a data set the size of which you're not clear on until the program is running (like needing 100 structures for a database instead of 10).


    For every malloc there should be a free:


    free(pointer);


    This will give back to the computer the memory you took from it with malloc. Some people suggest you can get away with not doing this. I think it's just stupid and lazy and poor coding style.


    Never free a pointer you didn't get from malloc. Never free a malloc-returned pointer twice. Never try to get at data through a pointer after you've freed it.




    : does the for loop go like this?

    : Do yo have to declare it several times or only once?




    You can use as many for loops as you like, and even have them inside each other. It depends how you want your looping controlled.



    : for (index=8; (index < 10); index++


    : printf("%d, index) ;


    : for.... should I stop this for loop? does a for loop only go once till invisibly come to false to 10?




    During the comparison phase that I told you about in a previous post, it either passes or fails the condition (in your case, you've said 'loop as long as index is less than 10). If it fails the test when it goes to do the comparison, the loop automatically ends.


    You _can_ end it prematurely in a bunch of ways, but the _simplest_ way is to let the for loop end on its own. Most times it's a lot more readable that way.




    : Do I have to declare as many times as 10 goes up to?


    '10' doesn't go up to anything. In your loop, provided you fix it up, the following events will occur (starting as the program reaches the for loop).


    1) index will be set to 8. (initialization)

    2) is index less than 10? Yes. Do the loop. (comparison)

    3) print out the value of index.

    4) increment index by one. index is now 9. (alteration)

    5) is index less than 10? Yes. Do the loop. (comparison)

    6) print out the value of index.

    7) increment index by one. index is now 10. (alteration)

    5) is index less than 10? No. Go to the instruction just after the loop. (comparison)



    : Should I use break to break out of a for loop earlier?




    You _can_ use break to leave the nearest enclosing for loop (or any C loop) before it will 'naturally end'. It's up to you to determine if you need to do this.



    : Just answer that question, should I only write a for loop ones or 10 times? from 8?




    How about a 'please' for the second time?


    It depends what you want to do. If you want a simple single counting loop that starts at some number and ends at some number, you only need a single for loop.



    : How does strlen give you the size of a string? from printf? do you have to print it out? studdy:



    strlen has nothing to do with printf. They are two separate functions (routines, procedures). You give strlen a C-style string, it gives you back the length. You can do anything you want with the value. Print it out, assign an integer variable to it, whatever.




    : void main()

    : }


    : int index;

    : int *ptr;

    : ptr=malloc(2) ;


    Don't do this. See the comments at the top of the program.




    : *ptr=index;


    the variable index hasn't been set to anything yet, so copying that value to the space 'ptr' points to is meaningless.




    : printf("%d
    ", index) ;


    This will print out garbage because of the previous reason.




    : index=5;


    The previous line is redundant because you're doing it in the initialization part of the for loop. It is, however, correct C, and totally harmless.


    : for (index=5; (index < 7); index++

    : printf("%d, index) ;


    Great, so it prints out the values 5 and 6 (provided you fix your printf statement).


    : break;


    Since this break isn't inside a for loop, it's meaningless. It has nothing to do with the previous for loop, which will stop on its own anyway.




    : printf("%d, index) ;


    This will print out the value 7, since it was the last value index had so that it could fail the comparison in the for loop and fall out of the loop.




    : puts("Its now incremented to 6") ;


    Properly written, but not true. index is now 7, not 6. If it were 6, the for loop would still be executing.




    : _________________________________


    : char *string1[20]="Hellow";


    : does the * before the string tell it to point to first element of the string, [0]? which is 'H' ?




    This is a meaningless C statement. You're telling the compiler that string1 is an array of pointers, and then you're trying to assign that to a single string. (Yes, you can have arrays of pointers, pointers to arrays, pointers to basic C types, pointers to structures, arrays of pointers to arrays of structures, ad nauseum, and THEY'RE ALL DIFFERENT TYPES).


    You might be being confused by the different uses of array brackets and asterisks. In this case:


    int i;

    int *p;

    p=&i;


    the asterisk * is part of the variable p's type (we're declaring a pointer-to-integer here). However in this case:


    *p=3;


    the asterisk * is dereferencing the pointer p. The two uses are related, but they are NOT THE SAME.


    The same thing goes for array brackets. Here:


    int k[3];


    the brackets are part of the variable k's type (we are declaring an array-of-int here). However with this:


    k[2]=6;


    the array brackets are saying to the compiler 'an index follows, so what preceeds must be an array'. Again, the uses are related, but they are NOT THE SAME.


    You must keep separate the distinctions.


    This is made more confusing the more complex your types are. It is also made confusing by the ability to initialize variables (including arrays) as they are being declared.


    You're getting awfully complex with your attempts to understand pointers. You're wandering into very confusing territory. You may wish to slow down a bit before you get totally confused. Then again, you may be a lot better at understanding pointers than I was when I started. :)





    : thanks.


    No problem. Good luck.




  • : jdslkfjkljdsl;fkkjdlkfjl;kdj;asdd

    fsdl

    'f'ddjf

    sddfj

    sdd

    fs

    ddfj

    sdfj

    djf

    sd

    fjsdfjd

    f

    lsdjf

    lsdjf

    d

    fjsd

    dfj

    dfl

    asdal

    faajsd

    fjld

    sdf

    dsjfj

    dsj

    f

    sdf

    dsfj


    djsfl

    jldsj

    fl

    sdjf

    ads

    lfj

    ldsjfk

    djsf

    dsfa

    jl

    sdjlfjl

    sdjklf

    aljdsa

    ljfa

    ds

    faljdsjjfalkdsdjf

    sdjf

    dasj

    fjsldkfj

    sdf

    ds

    jfsdjfklsdfj

    sd

    jsdf

    sdfjk

    jf

    sdfj

    sddfj

    sdjf

    asdfj

    saddjf

    sdjf

    sddjf

    sddjfl

    sdjf

    sdjflsdkjdf

    sdjflkasj

    dflkjsdlddj

    jkl

    dsfjl

    alfjl

    sdajf'sdaj

    fasd

    dfjsd

    fs

    adfjsa

    df

    saddfj

    sdjf


    sdjf

    sdjf

    sd

    fsdfsd'

    f'


    sd

    f

    sd

    f

    sdd

    f

    sd

    f

    asd


    f

    sd

    f

    sd

    f


    sd

    f

    asda

    fa

    dsaa

    fa

    sd

    f


    f

    sdf


    sdf




    sdf

    sd

    f

    ds

    f

    ds

    f

    sd

    f

    ds

    f

    sd

    f

    sd

    f

    d

    sf

    ds

    f

    ds

    f

    sd

    f

    sd

    f

    ds

    f

    ds

    f

    sd

    f

    s

    df

    sd

    f


    sdf

    s

    df

    dsf


    sdf

    d

    f

    dsf


    dfs

    f


    fd

    fd

    fd


    fd

    fd


    fd

    sd

    f

    sdf


    sdf

    s

    d

    f

    sd

    f


    sdf


    sdf

    sd

    f

    sdf


    fsd

    ds

    d

    d




    ds

    f

    sad

    f

    sd

    f

    sd

    df

    sd

    f

    sd


    ds

    a

    df

    s

    d

    sd

    f

    sd

    fs

    da

    f

    sd

    f

    s

    sdf

    s

    df


    sdf


    sd

    f

    d

    f

    d




    df

    d

    sf

    s

    df

    sd

    f

    asd

    f


    dsf



    fsd


    f

    sd

    f

    sdf

    s

    df

    sd

    f

    sd

    fsd

    f

    sd

    f

    sd

    df

    sdf


    sdf

    sd

    f

    sd

    f

    sd

    f

    sd

    f

    sd

    f

    sd

    f

    sd

    f

    sd

    f

    d

    sf


    dsf




    sd

    f

    dsf

    sd

    f

    ds

    f

    sdf


    sdf


    sdf


    dsfd

    sdf

    sd


    dfs

    fd


    fd

    sdfd

    s

    df

    ds

    f

    df


    df

    df


    dsf




    dsf


    dsf


    fsd

    sd

    f

    dsf


    dfs

    dfs

    sd

    df


    sdf

    sddf


    sdf


    d

    fs

    sdf




    sdf

    sd

    f

    dsf


    dsf


    sdf


    sdf


    as

    df

    s

    df

    fsd

    s

    df

    dfs


    fds

    fds

    df


    f

    d


    df


    df

    d

    f

    d

    fs

    d

    f

    asd

    fs

    adf

    as

    ddf

    s

    adf

    sd

    f

    sd

    f


    df


    sdf


    f

    f


    f

    f


    f


    f


    f

    f


    f


    f




    f


    f


    f


    f


    f


    f

    f


    f

    f


    f

    f

    f


    f

    f


    ff


    f


    f

    f


    f


    f


    f


    f

    f


    f


    f


    ds

    fa

    df


    sfd


    sd

    fa

    sdadf

    sad

    f

    sd

    f

    asdf

    asad

    f

    s

    adf

    sd

    f

    sdd

    f

    sd

    f

    sd

    f


    sdf

    s

    df

    sd

    f


    sd

    f

    sdf


    sdf

    sd




    f

    sd


    sd

    f

    sdf


    sdf

    sd

    f


    s

    df

    s

    df

    sd

    f

    sd

    f

    sd

    f


    s

    f

    sdf


    dsf




    df

    d

    sf

    ds

    f

    d

    sf

    ds

    f

    s

    df

    s

    df

    s

    da

    fa

    sd

    f

    sd

    ff

    sd




    f

    sdd

    f


    d




    dddddddddddddddd

    sdf

    d







    dfds

    f

    sd

    f

    ds

    f

    ds

    f

    dsf


    dsf

    d

    sf

    d

    d


    ds

    d


    d

    d

    d

    d


    d

    d


    d

    d


    d

    d


    d

    d


    d

    d


    d

    d


    d




    d

    d

    d


    d


    d

    d

    d


    d

    d


    d

    d

    d

    d


    d

    d


    d

    d


    d

    d

    d


    d

    d


    d

    d


    d




    d

    d




    d

    d


    wef


    d

    f


    d

    d


    d


    d

    d


    d


    d

    d


    d


    d


    d


    d

    d


    d


    d

    d


    d


    d

    d

    d


    d

    d


    d

    d


    d

    d

    d


    d


    d

    d


    d


    d


    d


    d


    d


    d


    d

    d


    d

    d


    d


    d


    d

    d


    d


    d

    d


    d


    d


    d

    d


    d


    d


    d


    d


    d

    d


    d


    d


    d


    d

    d


    d


    d


    d


    d




    d

    d


    d


    d


    d


    d




    d


    d


    d


    d

    d


    d


    d

    d

    d


    d

    d


    d

    d


    dd

    d


    d


    d

    d


    d


    d

    d


    d

    d

    d


    d


    d

    d

    d


    d

    d


    d



    d




    dd

    d


    d


    d



    d

    dd


    d


    d


    dd

    d


    d

    d


    d

    d


    d


    dd


    d


    d

    d




    d

    d


    d

    d


    d


    d




    d


    d


    d

    d


    d


    d


    d

    d




    d


    d

    d

    d


    d

    d

    f

    d

    d


    d

    d


    d


    d


    d


    d

    d


    d


    d


    dd


    d


    d




    d


    d


    d


    d


    d


    d


    d


    d


    d


    d


    d


    d

    d

    ;d;

    d;

    'd

    d


    d


    dd


    d

    d


    d


    d

    d


    d

    d


    d


    d




    d


    dd


    d


    d


    d

    d


    d


    d




    d





    d

    d


    d

    d



    d

    d




    d


    d

    d


    d


    d

    d


    d































    ddddddd
















































    :

    : : int *pointer;

    : : pointer=





Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories