Jump to content

A few questions on my mind..

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
9 replies to this topic

#1
Moudi

Moudi

    Programmer

  • Members
  • PipPipPipPip
  • 167 posts
I have been wandering around trying to understand a few things but can't get a hold of it:
first of all the
-> statement.
using ralloc and malloc.
And using structures.

the structure question i'm wondering is just what this line means:

#include <stdio.h>


int main(void)

{

    struct point

    {

        int x;

        int y;

    } [B][I]x = {1, 2}, y = {3, 4};[/I][/B] //this line


    printf("%d, %d, %d, %d\n", x.x, x.y, y.x, y.y);

}



#2
solidsnake

solidsnake

    Newbie

  • Members
  • Pip
  • 5 posts
That line is immediately making two instances of that structure.

#3
SolidState

SolidState

    Learning Programmer

  • Members
  • PipPipPip
  • 38 posts
Seems like that is defining two points; x and y. This is why you have an assignment of two values, the x and y coordinates for point x and for point y.

#4
SolidState

SolidState

    Learning Programmer

  • Members
  • PipPipPip
  • 38 posts
Solids ftw lolz

#5
Moudi

Moudi

    Programmer

  • Members
  • PipPipPipPip
  • 167 posts
What about "->" and malloc and ralloc.
I can make my way threw structures. like lets say using ralloc.
I need a user to input a string.
But i don't know how big his input can be right ?
So what to define the variable ?
I usually just put char a[1000000000000];
This line is replaced by what ?

#6
SolidState

SolidState

    Learning Programmer

  • Members
  • PipPipPip
  • 38 posts
There is no memory allocation taking place in your code example. The point is not an Array it has two independent variables so you have have to allocate memory the size of the Point itself, so you have a pointer to the struct and not necessarily a pointer directly to the specific variable.

#include <iostream>
#include <nameofpointsource>

Point thegoods = { 1, 2 };
Point * p_point = thegoods;

//You can access the variables of the point structure by using "x->x" or "x.x" dot notation
cout << p_point.x;

Make more sense now?

Edited by SolidState, 14 February 2010 - 12:39 PM.
Goofed


#7
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts

Moudi said:

What about "->" and malloc and ralloc.
What about 'em? They're pretty much unrelated. The -> operator is used to access members of a struct or union via a pointer. The functions malloc and realloc are used for memory allocation.

Moudi said:

I need a user to input a string.
But i don't know how big his input can be right ?
So what to define the variable ?
I usually just put char a[1000000000000];
This line is replaced by what ?
In C++, use a std::string. In C, use some initially-sized buffer (something more reasonable than your example). Then you choose to either discard excess input or use dynamic allocation to keep up with incoming data. Your choice makes it either simple or complex.

Assuming you mean the complex route of "infinite" input, you get to more or less replicate what a std::string does, reallocating more space as needed.

#8
solidsnake

solidsnake

    Newbie

  • Members
  • Pip
  • 5 posts
Now I think he didn't ask the question well. You actually allocate memory for two of those instances if you take it rough. If you take it to basic doing "int a" you are allocating that space in memory, indeed static but allocated. If I understood what you asked you are thinking at this:
point *x= new point{1,2}
point *y= new point{3,4}
and now you can access them as pointers -> and delete them because it's dynamic.

#9
Moudi

Moudi

    Programmer

  • Members
  • PipPipPipPip
  • 167 posts
I know how to input a string, but i/e i don't know how big it is ?
So how do i use malloc for that ?
like

int *a = sizeof(malloc(*a));
then what ?

#10
Sysop_fb

Sysop_fb

    Programmer

  • Members
  • PipPipPipPip
  • 160 posts
If you have a question on how to properly use a standard C function you can easily google for the man page.

Here's a very bland demo of malloc/realloc.

#include <stdio.h>

#include <stdlib.h>


int main()

{

	char *first, *tmp;

	

	if( (first = malloc(sizeof(char)*10)) == NULL)

	{

		printf("Allocation failed\n");

		exit(-1);

	}

	if( (tmp = realloc(first, sizeof(char)*15)) == NULL)

	{

		printf("Reallocation of data failed\n");

		free(first);

		exit(-1);

	}

	first=tmp;

	

	free(first);

	return 0;

}

You can read further by googling for 'Dynamic memory allocation in C' such as this website... Chapter 11: Memory Allocation