Thread: Help with a program to "mirror" using pointers and struct

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

    Help with a program to "mirror" using pointers and struct

    Hi,

    I have to write a program that basically takes in values and spits out it's mirror. What I mean is for example if the data is 123 the output would 123321, it prints itself and then itself backwards. This is part of a larger program....I will put in what I have....I know I'm close I'm getting a few compile errors....I just need a little help to get over this help.


    Code:
    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);
        }
    }
    
    struct Vector Mirror (struct Vector v)
    {
    
      struct Vector *mirror;
      int i = 0;
      int j = 0;
    
      AllocVector(mirror,v.size + v.size);
    for (i=0; i<v.size; i++)
      {
     v.mirror[i] = v.data[i];
      //v.data[i] = mirror[i];
      }
    j = i;
    
    for (i=v.size-1; i>=0; i--)
      {
      {
    mirror[j] = v.data[i];
    j++;}
    
    return mirror;
    }
    The compile errors I get are the following the refer to the assignment of mirror[i] to v.data[i] as well as j and the return. any help is greatly appreciated
    Code:
    vectora.c:84: incompatible types in assignment
    vectora.c:91: incompatible types in assignment
    vectora.c:94: incompatible types in return

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    struct Vector Mirror (struct Vector v)
    {
    
      struct Vector *mirror;
      int i = 0;
      int j = 0;
    
      AllocVector(mirror,v.size + v.size);
    for (i=0; i<v.size; i++)
      {
     v.mirror[i] = v.data[i];
      //v.data[i] = mirror[i];
      }
    j = i;
    
    for (i=v.size-1; i>=0; i--)
      {
      {
    mirror[j] = v.data[i];
    j++;}
    
    return mirror;
    }
    This wants you to return a structure.
    That is not a structure, but a pointer to a structure.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help assignment due tomorrow
    By wildiv in forum C Programming
    Replies: 6
    Last Post: 01-27-2010, 08:38 PM
  2. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  3. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM