Solved

how can i store data in to a array

Posted on 2010-11-30
8
324 Views
Last Modified: 2012-05-10
hallo,
im trying to put data in to array its a name

but i dont know how

please help.
#include <stdio.h>
#include <conio.h>
int main (void)
{
 char vnaam[50];
printf("Wat is u voornam ?");
scanf("%s",vnaam[50]);
getchar();
printf("well look %s",vnaam[50]);
getchar();    
}

Open in new window

0
Comment
Question by:kensy11
8 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
Comment Utility
You don't need to specify the array index when writing to it:
#include <stdio.h>
#include <conio.h>
int main (void)
{
 char vnaam[50];
printf("Wat is u voornam ?");
scanf("%s",vnaam);
getchar();
printf("well look %s",vnaam);
getchar();    
}

Open in new window

0
 
LVL 20

Accepted Solution

by:
BuggyCoder earned 125 total points
Comment Utility
change the following statement:
scanf("%s",vnaam[50])
with
scanf("%s",vnaam)
0
 
LVL 32

Assisted Solution

by:phoffric
phoffric earned 250 total points
Comment Utility
fyi - vnaam[50] in the scanf is simply the 51st entry into the vnaam array.
But since you define vnaam[50] to have only 50 entries, then you are actually referencing a char that is outside of the array. In some instances, your program will crash with a segment fault if you do this.
0
 

Author Comment

by:kensy11
Comment Utility
thanks my code looks like this

i have another question what if i dont know how long the array will be, now its 50 but can just leave it blank ?



#include <stdio.h>
#include <conio.h>
int main (void)
{
 char vnaam[];
printf("Wat is u voornam ?");
scanf("%s",vnaam);
getchar();
printf("well look %s",vnaam);
getchar();    
}

Open in new window

0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 32

Expert Comment

by:phoffric
Comment Utility
>> char vnaam[];
Sorry, this is not valid. If you are filling up an array with scanf, you should make it much larger than any possible entry. After the entry, it will be null terminated.
0
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 125 total points
Comment Utility
I don't think you can. Normally you would size the char[] to the maximum size you were expecting.

#define MAX_STRING_SIZE 80

int main()
{
    char[MAX_STRING_SIZE] buffer;

    // scanf, etc, etc
}
0
 
LVL 32

Assisted Solution

by:phoffric
phoffric earned 250 total points
Comment Utility
To protect from overflow, you should consider using fgets which limits the number of chars transferred to the buffer. (In this case, from the console, you would use stdin as the file handle.)
   http://www.cplusplus.com/reference/clibrary/cstdio/fgets/

But read this carefully w.r.t. newline char and null termination.
0
 
LVL 17

Expert Comment

by:CSecurity
Comment Utility
I'm not sure about scanf function but I remember that cin function in C++ takes 512 characters, not more.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now