Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8668413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:12:28+00:00 2026-06-12T18:12:28+00:00

Hello i tried to compile my code but i get acess violation error. Im

  • 0

Hello i tried to compile my code but i get acess violation error. Im trying to make an agenda that i can insert values using a list. What is the error in my code?

#include <stdio.h>
#include <iostream>

using namespace std;

typedef struct ap_agenda{
    char *name;
    char *telefone;
    struct ap_agenda *proximo;
};

void init(ap_agenda* lista){
    lista = NULL;
}

void insere(char *nome, char *telefone, ap_agenda* lista){
    ap_agenda *p;
    p = (ap_agenda*) malloc(sizeof(ap_agenda*));
    p->name = nome;
    p->telefone = telefone;

    if(lista == NULL){
        lista = p;
    }else{
        lista->proximo = p;
    }
}

void imprime(ap_agenda *lista){
    cout << lista[0].name << endl;
}

int main(){
    ap_agenda agenda;

    init(&agenda);
    insere("test","123456",&agenda);
    imprime(&agenda);

    system("pause");
}

Thanks !

Hello, thanks for the answers! I changed my code and now its “working” but when i try to print the list, its jump one line.

void insere(std::string nome, std::string telefone, ap_agenda* lista){
ap_agenda *p = new ap_agenda;

p->name = nome;
p->telefone = telefone;
p->proximo = NULL;

if(lista == NULL){
    lista = p;
}else{
    while(lista->proximo != NULL)
        lista = lista->proximo;

    lista->proximo =  p;
    }
}

void print(ap_agenda* lista){
    ap_agenda *p;
    for(p=lista; p!=NULL; p=p->proximo)
        cout << p->name.c_str() << endl;
}

The output is:
[blankline]
test1
test2

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T18:12:30+00:00Added an answer on June 12, 2026 at 6:12 pm

    The access violation probably comes from your call to insere which isn’t working like you think it does.

    int main(){
        ap_agenda agenda; //<-- local variable lives on the stack
    
        init(&agenda); //<-- passes the address of the local variable
    

    When this is passed to init:

    void init(ap_agenda* lista){ // lista is a temporary variable that contains a
                                 // copy of the address
        lista = NULL; //<-- this overwrites the value in the temporary variable.
    } // when this function returns, the temporary variable is destroyed.
    

    At this point agenda had not been modified or initialized in any way. Now you pass the address of agenda off to insere.

        insere("test","123456",&agenda);
    

    insere is defined

    void insere(char *nome, char *telefone, ap_agenda* lista){
        ap_agenda *p;
        p = (ap_agenda*) malloc(sizeof(ap_agenda*)); // you allocate a new `ap_agenda`
                                                     // pointer. not enough for a struct
        p->name = nome; // initialize name (probably ok but not what you expect)
        p->telefone = telefone;  // initialize telefone (possible access violation)
    
        if(lista == NULL){ // since lista is the address of a stack variable it won't
                           // be NULL here
            lista = p;
        }else{
            lista->proximo = p; // this sets the allocated struct to the `proximo` member
                                // of the stack variable that was passed in
        }
    }
    

    Note that when this returns, nome and telefone of the stack variable agenda have not been initialized.

        imprime(&agenda);
    

    When the address of the stack variable agenda is passed to imprime it tries to print the value of name which has not been initialized.

    void imprime(ap_agenda *lista){
        cout << lista[0].name << endl; // possible access violation
    }
    

    If instead, you passed in the proximo member of agenda which was initialized in insere you would see the name value printed.

        imprime(agenda->proximo);
    

    However, as others have pointed out, there are a lot of other issues in this code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I tried running a python script: print Hello, World! And I get this error:
Hello you naughty code animals.. I have tried to get this code to work,
This is what I get when trying to compile a simple hello world program
When trying to compile with Code::Blocks the example that comes up with GTK+: #include
I'm trying to get an app to compile that uses the android-support-v4.jar and the
Hello friends I have tried many times but I am not successful than please
hello i am having same issue, i tried your solution but it didnt help
When I tried to do a simple modification of the hello android program that
I tried the following code tree. If I put the header file hello.h into
I wanted to use boost::thread in my program, but get the following compiler error

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.