Jump to content

C - "Access violation writing location" when inputing string

Go to solution Solved by Zalosath,
1 hour ago, Matttaj69 said:

Hi,

I am working on a simple code in C, have got the logic almost sorted.

However, in order for my program to work, I need a user to input a string. I have tried <scanf_s("%s", potr);> (shown in picture), but while debugging, it has ended on that line with "Access violation writing location" error. I have tried putting the string directly as a parameter in the function call, which worked perfectly fine.

Any ideas what could be wrong?

Notice: long story short, I must use Microsoft Visual Studio and the "safe" version of scanf.

 

Screenshot of the part of my code:

image.png.9c0219065ac2466cad2aece0ee65ad8f.png

 

Thanks in advance

You need to enter the length of the buffer for scanf_s.

scanf_s("%s", potr, sizeof(potr))

 

Hi,

I am working on a simple code in C, have got the logic almost sorted.

However, in order for my program to work, I need a user to input a string. I have tried <scanf_s("%s", potr);> (shown in picture), but while debugging, it has ended on that line with "Access violation writing location" error. I have tried putting the string directly as a parameter in the function call, which worked perfectly fine.

Any ideas what could be wrong?

Notice: long story short, I must use Microsoft Visual Studio and the "safe" version of scanf.

 

Screenshot of the part of my code:

image.png.9c0219065ac2466cad2aece0ee65ad8f.png

 

Thanks in advance

Link to post
Share on other sites

1 hour ago, Matttaj69 said:

Hi,

I am working on a simple code in C, have got the logic almost sorted.

However, in order for my program to work, I need a user to input a string. I have tried <scanf_s("%s", potr);> (shown in picture), but while debugging, it has ended on that line with "Access violation writing location" error. I have tried putting the string directly as a parameter in the function call, which worked perfectly fine.

Any ideas what could be wrong?

Notice: long story short, I must use Microsoft Visual Studio and the "safe" version of scanf.

 

Screenshot of the part of my code:

image.png.9c0219065ac2466cad2aece0ee65ad8f.png

 

Thanks in advance

You need to enter the length of the buffer for scanf_s.

scanf_s("%s", potr, sizeof(potr))

 

Main PC [ CPU AMD Ryzen 9 7900X3D with H150i ELITE CAPPELIX  GPU Nvidia 3090 FE  MBD ASUS ROG STRIX X670E-A  RAM Corsair Dominator Platinum 64GB@5600MHz  PSU HX1000i  Case Lian Li PC-O11 Dynamic  Monitor LG UltraGear 1440p 32" Nano IPS@180Hz  Keyboard Keychron Q6 with Kailh Box Switch Jade  Mouse Logitech G Pro Superlight  Microphone Shure SM7B with Cloudlifter & GoXLR ]

 

Server [ CPU AMD Ryzen 5 5600G  GPU Intel ARC A380  RAM Corsair VEGEANCE LPX 64GB  Storage 16TB EXOS ]

 

Phone [ Google Pixel 8 Pro, 256GB, Snow ]

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×