I have a question regarding linked-lists/structs in C. I am trying to add vertices to a linked list and populate them with data.
Why do I get an access violation at newHead->name = newVertexName even though I assgined memory to newHead beforehand?
Code for context:
typedef struct Vertex Vertex;
typedef struct Vertex
{
char name;
Vertex *next;
} Vertex;
struct Vertex* AddVertex(Vertex* head, char newVertexName)
{
Vertex* newHead = malloc(sizeof(Vertex));
newHead->name = newVertexName; // Access violation occuring here
newHead->next = head;
return newHead;
}
int main()
{
char s[100];
const int nNrOfVerts = 27;
Vertex* adjList[28];
for(int i = 0; i <= nNrOfVerts; ++i)
{
adjList[i] = NULL;
}
for(int i = 1; i <= nNrOfVerts; ++i)
{
if(scanf("%s", s) == 1)
{
adjList[i] = AddVertex(adjList[i], s[i-1]);
}
else
{
break;
}
}
return 0;
}
thank you!
mallocis succeeding on every call?s[i-1]-->s[0]