-2

I have created a structure and I want to give it a value without creating an object (struct integer a). In string I can use strcpy for it but what can I do for integer values.

#include<stdio.h>
struct integer
{
   int x[5];
};

main()
{
  struct integer *pointer;
  pointer->x[0]=5;
  printf("pointer->x[0]");
}
log N
  • 925
  • 9
  • 33
anonymous
  • 11
  • 3

7 Answers7

1

The problem is you didn't make the poiter point to any object, so either use malloc or use it like this:

int main(){
    struct integer obj;
    struct integer *pointer = &obj;
    pointer->x[0]=5;
    printf("%d\n",pointer->x[0]);
}
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
1

As pointer is a pointer you need to allocate memory before using it.

do

pointer = malloc(sizeof(*pointer));
pointer->x[0]=5;
Rohan
  • 52,392
  • 12
  • 90
  • 87
0

First make the pointer to point to some allocated space.

#include<stdio.h>
struct integer{
int x[5];
};

int main(){
struct integer *pointer = malloc(1*sizeof(struct integer)); //Allocated memory 

pointer->x[0]=5; //Initialize the element(s) of x

printf("%d",pointer->x[0]); //Use proper format specifier for printf 

free(pointer); //Always free memory after use.

return 0;

}
P0W
  • 46,614
  • 9
  • 72
  • 119
0

As you are living in the C world you need to use malloc/free.

i.e.

#include<stdio.h>
struct integer{
int x[5];
};
main(){
struct integer *pointer;
pointer = malloc(sizeof(struct integer));
pointer->x[0]=5;
printf("pointer->x[0] = %d", pointer->x[0]);
free(pointer);

}

Should have put in the indentation but could not be bothered to fire up my IDE

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
0

strcpy will not work for you if you have not allocated the corresponding memory beforehand.

There is also general function with similar properties to strcpy - memcpy it basically copies the memory pointed by one pointer to another.

#include<stdio.h>
#include <stdlib.h>
struct integer {
   int x[5];
};

main(){
   struct integer *pointer, source;
   source.x[0] = 5;
   pointer = malloc (sizeof(struct integer));
   memcpy(pointer, &source, sizeof(struct integer));
   printf("%d", pointer->x[0]); //will be 5
}
Boris Strandjev
  • 46,145
  • 15
  • 108
  • 135
-1

You cannot assign the value without creating the object , the code below can cause segmentation fault as it may be accessing memory not allocated to the program.

main(){
struct integer *pointer; //<-- 'pointer' is just some garbage address
pointer->x[0]=5; //<-- as pointer may contain memory which may not be accessible to current process which causes "Segmentation Fault"
printf("pointer->x[0]");
}

@Ed heal: thanks for correcting me

Either you use [lang: C++]struct integer *pointer=new integer(); [lang: C] pointer = (struct integer*) malloc(sizeof(struct integer)); to allocate memory or do not use pointer, like below

main(){
struct integer *pointer = (struct integer*)malloc(sizeof(struct integer)); //<-- 'pointer' is now pointing to memory allocated for struct 'integer'
pointer->x[0]=5;
printf("pointer->x[0]");
}
Xinus
  • 29,617
  • 32
  • 119
  • 165
  • 2
    It is tagged as C, not C++ - hence cannot use new – Ed Heal Aug 11 '13 at 06:50
  • 2
    You should not cast the result of malloc in C - see [this answer](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) for a fuller explanation. – Paul R Aug 11 '13 at 07:17
-1

The problem is that your pointer didn't point to any object!!!!

You have 3 options:

use malloc like this

struct integer *pointer = (struct integer*)malloc(1*sizeof(struct integer)); 

OR

main()
{
struct integer nonPointer;
nonPointer.x[0]=5;
printf("pointer->x[0] %d",nonPointer.x[0]);
}

OR

 main()
 {
    struct integer integerObj;
    struct integer *pointer=&integerObj;
    pointer->x[0]=5;
    printf("pointer->x[0] %d",pointer->x[0]);
 }

Now about the copy, if you need to copy object (like strcpy with string ) you can use memcpy:

void *memcpy(void *str1, const void *str2, size_t n)

in your case you can use it like this:

struct integer *target, *source;
target=(struct integer*)malloc(1*sizeof(struct integer));
source=(struct integer*)malloc(1*sizeof(struct integer));
source->x[0]=5;

memcpy(target, source, sizeof(struct integer));
printf("target->x[0] %d",target->x[0]);
One Man Crew
  • 9,420
  • 2
  • 42
  • 51
  • You should not cast the result of `malloc` in C – Paul R Aug 11 '13 at 07:06
  • malloc return void* you must cast the result! – One Man Crew Aug 11 '13 at 07:10
  • 1
    No - that's only true for C++ - in C you don't need to cast a `void *` and it is considered potentially dangerous to do so unnecessarily, as it can mask errors that would otherwise be reported via compiler warnings. See [this answer](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) for a fuller explanation. – Paul R Aug 11 '13 at 07:12
  • The problem comes in when you fail to include stdlib.h, and the compiler initially assumes that malloc returns an int. The real problem is, you DONT get any warning from the compiler.You merrily then convert that int to a struct intege* (via the cast). On machines where sizeof(struct integer*) is different from sizeof(int), the code is seriously broken. Now if you just have struct integer *var = malloc( 10 ); And you miss out the include , you will get a warning from the compiler. – One Man Crew Aug 11 '13 at 07:14