Scalar operand

This is a discussion on Scalar operand within the C Programming forums, part of the General Programming Boards category; I have been working on this program, and I finally have it down to just a few problems that I ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    2

    Scalar operand

    I have been working on this program, and I finally have it down to just a few problems that I cannot figure out. We were to write everything except the main() function and two header files are given to us to use. The first one sets up an array. We are not supposed to know what goes into the array. The following is a structure I created to implement that array:

    typedef struct TotalRecord{
    32 | ArrayElement *myarray;
    33 | int size;
    34 | }ArrayADT;
    35 |
    36 | typedef ArrayADT *ArrayADTPtr;

    I am receiving the following error in one of my functions:

    ArrayADTPtr CreateArray(int size)
    51 |
    52 | {
    53 | int i;
    54 |
    55 | ArrayADTPtr newArray;
    56 | ArrayElement elem;
    57 |
    58 | newArray =(ArrayADTPtr) malloc(sizeof(ArrayADTPtr));
    59 | elem = (ArrayElement) malloc(size*sizeof(ArrayElement));
    "assignment1.c", line 59.17: 1506-117 (S)Operand must be a scalar type.

    I am not sure what the problems is, which operand needs to be scalar, or what this program is getting at. I am very new at this. Please help me if you can. Thank you.

    kfarinella

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    try following
    Code:
        newArray =(ArrayADTPtr*) malloc(sizeof(ArrayADTPtr)); 
        elem = (ArrayElement*) malloc(size*sizeof(ArrayElement));
    
        /* note use of pointer * with (ArrayADTPtr*) and (ArrayADTPtr*) */
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    2

    Question problems still

    I did that, but then it said that I had a Pointer to a pointer, not just a pointer. Here is the full code, if that can help.

    #include <stdio.h>
    #include "/showme/cecs203d/assign/assign1/ElementType.h"

    typedef struct TotalRecord{
    ArrayElement *myarray;
    int size;
    }ArrayADT;

    typedef ArrayADT *ArrayADTPtr;
    #include "/showme/cecs203d/assign/assign1/ArrayADT.h"

    ArrayADTPtr CreateArray(int size)

    {
    int i;

    ArrayADTPtr newArray;
    ArrayElement elem;

    newArray =(ArrayADTPtr) malloc(sizeof(ArrayADTPtr));
    elem = (ArrayElement) malloc(size*sizeof(ArrayElement));

    for (i=0; i<size; i++)
    newArray[i].size = size;

    if (!newArray)
    return(NULL);
    return (newArray);
    }


    int SetElement(ArrayADTPtr array, int i, ArrayElement element)

    {
    if(!array[i])
    {
    return(1);
    }
    array[i].myarray = element;
    return(0);
    }

    void DestroyArray(ArrayADTPtr *arrayPtr)

    {
    free(*arrayPtr);
    return;
    }

    int GetSize(ArrayADTPtr array)

    {
    return(array.size);
    }


    int GetElement(ArrayADTPtr array, int i, ArrayElement *elementPtr)

    {
    if(!array[i])
    {
    return(1);
    }
    elementPtr = array[i].myarray;
    return(0);
    }


    Can anyone help?
    Last edited by kfarinella; 02-13-2002 at 10:57 AM.

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    The function malloc returns a pointer. So you should declare elem as:

    ArrayElement *elem;

    And then you can allocate memory as:

    elem = (ArrayElement *) malloc (size*sizeof(ArrayElement));

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. *operator overloading: scalar matrix multiplication
    By gemini_shooter in forum C++ Programming
    Replies: 4
    Last Post: 06-08-2009, 02:14 PM
  2. delete zero out its operand
    By George2 in forum C++ Programming
    Replies: 2
    Last Post: 03-27-2008, 12:55 AM
  3. error: braces around scalar initializer...
    By Osiris990 in forum C++ Programming
    Replies: 2
    Last Post: 02-27-2008, 03:22 PM
  4. Help! About Type Conversions
    By Antigloss in forum C Programming
    Replies: 1
    Last Post: 05-28-2006, 10:24 PM
  5. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 01:56 PM

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