0 Replies - 274 Views - Last Post: 30 April 2021 - 12:05 PM Rate Topic: -----

#1 Jleger91   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 56
  • Joined: 19-August 20

subscripted value is neither array nor pointer nor vector

Posted 30 April 2021 - 12:05 PM

SOLUTION: Alright, having distanced myself a bit I can see what went wrong.
I wasn't assigning d.name to anything. It should look like this:

                        head -> d.name[i] = name[0][i];

The solution found me faster than expected. I just needed to look at the problem from a distance/different angle.

The Post:

I've underestimated the assignments from school. What seemed like an hour job is an entire days process and probably even longer. The pressure and frustration that results I would not wish upon any living human being.

list.c: In function ‘create_l’:
list.c:34:21: error: subscripted value is neither array nor pointer nor vector
head -> d.name[0][i];

    header file 

/* includes */
#include <stdio.h>
#include <stdlib.h>
#define NUM_LINKS       0

/* DATA structure */
typedef struct {
        char name[10];
        int age;
        int weight;

/* linked list structure */
struct linked_list {
        DATA d; //d is object of structure DATA
        //since its not a pointer, we use the dot operator
        //when referring to the objects attributes
        struct linked_list *next;

typedef struct linked_list ELEMENT;
typedef ELEMENT *LINK;

//function prototypes here
LINK create_l(char name[][10], int age[], int weight[]);


#include "list.h"

int main()
        LINK head;
        char name[NUM_LINKS][10]; //n arrays of 10 characters
        int age[NUM_LINKS], weight[NUM_LINKS];

        head = create_l(name, age, weight);

        free(head);     //free memory
        return 0;       //return success value


#include "list.h"

//this uses recursion to fill linked elements with data
LINK create_l(char name[][10], int age[], int weight[])
        LINK head;
        int i;
        static int lnk_cnt = 0; //count to end of the chain

        //the three arrays have the same number of base elements
        //therefore we know when we are done adding to the list
        //when age meets NULL marker, all arrays have met NULL marker
        if(lnk_cnt == NUM_LINKS)
                return NULL;
                head = malloc(sizeof(ELEMENT)); //allocate memory for the link
                /* set data for the element */
                head -> d.weight = weight[0];
                head -> d.age = age[0];
                for(i = 0; i < 10; i++) //10 elements in each name array
                        //regarding current array of names, and
                        //the element i of that array 0-9
                        head -> d.name[0][i];
                //call next recursion here//
                return head;

This post has been edited by Jleger91: 30 April 2021 - 12:13 PM

Is This A Good Question/Topic? 1
  • +

Page 1 of 1