Overclock.net - Overclocking.net
     
 
Home Gallery Reviews Blogs Register Today's Posts Mark Forums Read Members List


Go Back   Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming > Application Programming

Reply
 
LinkBack Thread Tools
Old 2 Weeks Ago   #1 (permalink)
New to Overclock.net
 
Join Date: Oct 2009
Posts: 5

Rep: 0 loonyt7 Unknown
Unique Rep: 0
Trader Rating: 0
Default Array program in C

I have the following code and I am not sure that its working right. I am using C. The final result is that the program is suppose to give me the length of each string that is entered that is the second for loop. I do not think this part is working right. I have gone from having no code to what is below on my own I just need a little help making sure that this is correct. Any assistance would be greatly appreciated.

Below are the instructions that I am having problems with.

Each time the name is read I then have to measure the length and allocate a dynamical string large enough to hold a copy of it. You will then copy the contents of buffer into this newly allocated string and add its
address to the array names. Once you are done filling the array names, each of its element should contain the address of a dynamically allocated copy of each word entered by the user. You will iterate over names and display these strings with a message.Once done, you will deallocate the memory you allocated

I am getting closer. The code now gives me a value of 2 for any input I put in it doesn't matter the length of word.

Code:
#include <stdio.h>
#include <string.h>

int str_length(char* names);
void copy(char *buffer, const char *names);

int main(void)
{
    
    int n = 0;
    char names[n];
    int i = 0;
    char buffer[256];
    
    printf("How many names to store: ");
    scanf("%d", &n);
    
    for( i = 0; i < n; i++)
    {
         printf("names[%d] = ", i);
         scanf("%s", buffer);
         copy(buffer, names);

    }
    
    for( i = 0; i < n; i++)
    {
         printf("buffer[%d] = %s %d\n", i, buffer, str_length);
    }
    
    
        
    
    system("PAUSE");	
    return 0;
}

int str_length(char* names)
{
    char* start = names;
    
    while ( *names++);
    
    return names-start;
    
}

void copy(char *buffer, const char *names)
{
   for ( ; ( *buffer = *names ) != '\0'; buffer++, names++ ) ;
 
}

Last edited by loonyt7 : 2 Weeks Ago at 09:36 PM
loonyt7 is offline   Reply With Quote
Old 2 Weeks Ago   #2 (permalink)
New to Overclock.net
 
Join Date: Oct 2009
Posts: 5

Rep: 0 loonyt7 Unknown
Unique Rep: 0
Trader Rating: 0
Default

I made a minor change to this code. I know I am getting closer I wish someone could help me with this.
loonyt7 is offline   Reply With Quote
Old 2 Weeks Ago   #3 (permalink)
Case Modder
 
Spotswood's Avatar
 
Join Date: Jul 2008
Location: New Hampshire, USA
Posts: 236

Rep: 46 Spotswood is acknowledged by some
Unique Rep: 39
Trader Rating: 0
Default

For starters, what is your "source" and "destination" parameters of your copy() method? And are you sure you're passing the correct variables for the source and destination to the copy() method?
__________________
Rich
Custom Wooden Case Builder
Overclock.net Mod of the Month
Spotswood is offline   Reply With Quote
Old 2 Weeks Ago   #4 (permalink)
New to Overclock.net
 
Join Date: Oct 2009
Posts: 5

Rep: 0 loonyt7 Unknown
Unique Rep: 0
Trader Rating: 0
Default

I was able to get the code to work finally thanks though
loonyt7 is offline   Reply With Quote
Old 1 Week Ago   #5 (permalink)
Security Sleuth
 
Pooping^fish's Avatar
 
intel nvidia

Join Date: Jul 2007
Location: egypt
Posts: 1,267

Rep: 67 Pooping^fish is acknowledged by some
Unique Rep: 62
Trader Rating: 3
Default

What the eff is that mess?

Just use strlen(), and if you wish to write your own function then just loop on the input buff until a null is found.
while(buff[i]) { i++ };
for example.
__________________
Quote:
"O, hai! Want som pRon? Dwnlod ths kodk frst. Its teh bst pRonz ever, we prmis." -GibbyGano
Proud Member of the Linux Gaming Community
I am your friend.

System: ragequit
CPU
Q9550 4ghz @ 1.25v
Motherboard
Asus Max 2 formula
Memory
OCZ LV blade 1:1 950mhz
Graphics Card
8800gtx 610/1ghz
Hard Drive
7200.10 250gb
Sound Card
X-FI Extreme Music
Power Supply
750w Toughpower
Case
Lian li pc-65
CPU cooling
TRUE
GPU cooling
stock
OS
leetlinucks
Monitor
24" Westy
Pooping^fish is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools



All times are GMT -5. The time now is 12:26 AM.


Overclock.net is a Carbon Neutral Site Creative Commons License

Terms of Service / Forum Rules | Privacy Policy | DMCA Info | Advertising | Become an Official Vendor
Copyright © 2009 Shogun Interactive Development. Most rights reserved.
Page generated in 0.11800 seconds with 8 queries