Can someone help me with Struct Pointers....compile errors

This is a discussion on Can someone help me with Struct Pointers....compile errors within the C Programming forums, part of the General Programming Boards category; Hi, I have the following code.... when I try to complile I get a huge amount of errors. I'm not ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    75

    Can someone help me with Struct Pointers....compile errors

    Hi,

    I have the following code.... when I try to complile I get a huge amount of errors. I'm not sure why?? It looks right to me?? The program is pretty obvious what I am trying to do.... Sum vectors. Any help is greatly appreciated.


    Code:
    #include <stdio.h>
    
    
    Struct Vector{
    int size;            //global
    float * data;
    };
    
    #include <stdio.h>
    
    Void AllocVector (struct Vector *VP int n)
    {
    VP -> size = N;
    VP -> data = malloc (N *sizeof float);
    }
    
    Void FreeVector (struct Vector *VP)
    {
    free (VP-> data);
    
    }
    
    int SumVector (struct vector * V)
    {
    int sum = 0;
    int i;
    for (i=0; i < v->size; i++)    {
    sum += v->data[i]; }
    
    return sum;
    }
    main ()
    {
    struct Vector v1; 
    const int N = 100; 
    AllocVector(&v1, N);
    
    if (v1->data != NULL) {
    v1->data[0] = 3.14;
    v1->data[1] = 3.15;
    
    }
    
    int sum = SumVector(&v1);
    
    FreeVect(&v1);
    }

  2. #2
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    460
    you forgot a comma
    Code:
    Void AllocVector (struct Vector *VP ,int n)
    and you called the pass-in size argument as the variable "n", not "N". C is case sensitive.
    Last edited by nimitzhunter; 03-06-2011 at 07:25 PM.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    75
    Hi thanks I am still getting a bunch of errors I made the adjustments you said... I will paste them...i can't imagine i have this much wrong...I'm hoping it's just a few things causing all of this.


    vector.c:4: parse error before `Vector'
    vector.c:4: syntax error before `{'
    vector.c:7: parse error before `}'
    vector.c:11: parse error before `AllocVector'
    vector.c:11: warning: `struct Vector' declared inside parameter list
    vector.c:11: warning: its scope is only this definition or declaration,
    vector.c:11: warning: which is probably not what you want.
    vector.c: In function `AllocVector':
    vector.c:13: dereferencing pointer to incomplete type
    vector.c:14: dereferencing pointer to incomplete type
    vector.c:14: parse error before `float'
    vector.c: At top level:
    vector.c:17: parse error before `FreeVector'
    vector.c:17: warning: `struct Vector' declared inside parameter list
    vector.c: In function `FreeVector':
    vector.c:19: dereferencing pointer to incomplete type
    vector.c: At top level:
    vector.c:24: warning: `struct vector' declared inside parameter list
    vector.c: In function `SumVector':
    vector.c:28: `v' undeclared (first use in this function)
    vector.c:28: (Each undeclared identifier is reported only once
    vector.c:28: for each function it appears in.)
    vector.c: In function `main':
    vector.c:40: storage size of `v1' isn't known
    vector.c:51: parse error before `int'
    [/CODE]

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    It's struct not Struct ... and it appears to be what set off at least half the other errors, since Vector was never correctly defined.

    C is case sensitive *everywhere*... Bob and bob are two different things...

  5. #5
    THANK YOU KINDLY SIR Phenax's Avatar
    Join Date
    Mar 2011
    Posts
    74
    Most of your stuff is right, but you've got a lot of typos (as others have said, C is case sensitive). Also, if your function doesn't modify your struct (if it only reads it), you don't need to pass a pointer to the struct. Not sure if the SumVector float->int thing was intended.
    Here's a slightly modified but working version:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct Vector {
    	int size;
    	float *data;
    };
    
    void AllocVector (struct Vector *VP, int n)
    {
    	VP->size = n;
    	VP->data = malloc(n * sizeof(float));
    	if(VP->data == NULL)
    	{
    		fprintf(stderr, "Memory allocation failed\n");
    		exit(1);
    	}
    }
    
    void FreeVector (struct Vector *VP)
    {
    	free (VP->data);
    }
    
    float SumVector (struct Vector v)
    {
    	float sum = 0;
    	int i;
    	for (i=0; i < v.size; i++)
    		sum += v.data[i];
    
    	return sum;
    }
    
    int main(void)
    {
    	struct Vector v1;
    	const int N = 2;
    	float sum;
    
    	AllocVector(&v1, N);
    	
    	v1.data[0] = 3.14;
    	v1.data[1] = 3.15;
    	sum = SumVector(v1);
    	
    	FreeVector(&v1);
    
    	printf("The sum of the vector is %.2f\n", sum);
    	return 0;
    }
    Quote Originally Posted by Plato
    Never discourage anyone...who continually makes progress, no matter how slow.

  6. #6
    Registered User
    Join Date
    Jan 2011
    Posts
    75
    Oh wow thank you kindly to everyone....so much help I really can't thank you enough....if someone has the time could they explain to me exactly what is going on in the code... Don't get me wrong I certainly know most of it....but there is a difference between being able to regirertate what I see in the book (with some modifications) and really understanding what is going on. I want to make sure I understand

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Sooo... now that Phenax did your homework for you, you want us to do the write-up too?

    Your best bet is to compile the code with debugging symbols included and run it through your debugging utility...

  8. #8
    Registered User
    Join Date
    Jan 2011
    Posts
    75
    Actually that was only maybe half of my homework...I have to write functions to Distribute the Vector, Mirror the Vector and reverse the Vector (This one I think I have close to done) I didn't mean for someone to write what the code is doing just generic overview....plus look at our two codes it's 85% mine....I wouldn't say he did it for me just helped me over a rough spot

  9. #9
    THANK YOU KINDLY SIR Phenax's Avatar
    Join Date
    Mar 2011
    Posts
    74
    Ask a specific question about what's going on in the code and I'll attempt to answer.
    The errors you made were very simplistic in comparison to the content of the code. You also lacked the basic skills to debug errors. If the compiler tells you there is a problem on line 4, even if it's not specific, you go to line 4 and see if there's anything wrong. Not identifying that there is case sensitivity in C is a very beginner issue. Missing things like commas, etc.. are also basic. You also passed 100 as the size which in the Add function would add in a ton of uninitialized values and divide by 100.

    For this code, you should know, or at least should figure out:
    Functions
    Structures
    Pointers
    Pointers to Structures (->, etc)
    The Heap (using malloc, free, etc)
    Last edited by Phenax; 03-06-2011 at 08:46 PM.
    Quote Originally Posted by Plato
    Never discourage anyone...who continually makes progress, no matter how slow.

  10. #10
    Registered User
    Join Date
    Jan 2011
    Posts
    75
    ok so I have a global Vector good for all functions right? It takes in a int and a pointer variable float variable, so it storing 4 bytes of memory each I believe? Then in VoidAlloc, I am deferencing size and data. Size is taking on the value of n and data is going to be the memory needed to allocate n I believe. Then write a function to free that vector..pretty simple. Next sumVector is where tell it to add data together. Then we have the main function which calls all the functions. So one question I have is why do we need to reference address the AllocVector and FreeVector and not SumVector? I think if I understand this it will help me with everything else

  11. #11
    THANK YOU KINDLY SIR Phenax's Avatar
    Join Date
    Mar 2011
    Posts
    74
    Quote Originally Posted by tmd2 View Post
    ok so I have a global Vector good for all functions right? It takes in a int and a pointer variable float variable, so it storing 4 bytes of memory each I believe? Then in VoidAlloc, I am deferencing size and data. Size is taking on the value of n and data is going to be the memory needed to allocate n I believe. Then write a function to free that vector..pretty simple. Next sumVector is where tell it to add data together. Then we have the main function which calls all the functions. So one question I have is why do we need to reference address the AllocVector and FreeVector and not SumVector? I think if I understand this it will help me with everything else
    I think you're right on about what you wrote. Glad to see you understand it pretty well.

    Basically, if you pass a pointer, you're doing that so you can modify the stuff at that address. If you don't need to actually modify the stuff at that address, there is no reason to pass it as a pointer. You can, and that is fine. Just make sure you use "->" instead of "." to access the members. I think that's more of just a stylistic decision, though.
    Quote Originally Posted by Plato
    Never discourage anyone...who continually makes progress, no matter how slow.

  12. #12
    Registered User
    Join Date
    Jan 2011
    Posts
    75
    ok great thanks a lot for the help very much appreciated.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  2. Replies: 1
    Last Post: 12-03-2008, 03:10 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  5. Pointers on pointers to pointers please...
    By Morgan in forum C Programming
    Replies: 2
    Last Post: 05-16-2003, 12:24 PM

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