Solved

how can i store data in to a array

Posted on 2010-11-30
8
328 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34242336
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
ID: 34242377
change the following statement:
scanf("%s",vnaam[50])
with
scanf("%s",vnaam)
0
 
LVL 32

Assisted Solution

by:phoffric
phoffric earned 250 total points
ID: 34242470
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:kensy11
ID: 34242774
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
 
LVL 32

Expert Comment

by:phoffric
ID: 34242949
>> 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
ID: 34242964
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
ID: 34243010
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
ID: 34250522
I'm not sure about scanf function but I remember that cin function in C++ takes 512 characters, not more.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

695 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